• 第七


     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:background="@drawable/bg"
     6     android:padding="16dp"
     7     android:orientation="vertical">
     8     <LinearLayout
     9         android:layout_marginTop="130dp"
    10         android:layout_width="match_parent"
    11         android:layout_height="wrap_content">
    12         <TextView
    13             android:layout_width="wrap_content"
    14             android:layout_height="wrap_content"
    15             android:text="姓  名 :"
    16             android:textSize="18sp" />
    17         <EditText
    18             android:id="@+id/et_name"
    19             android:layout_width="match_parent"
    20             android:layout_height="wrap_content"
    21             android:hint="请输入姓名"
    22             android:textSize="16sp" />
    23     </LinearLayout>
    24     <LinearLayout
    25         android:layout_width="match_parent"
    26         android:layout_height="wrap_content"
    27         android:layout_marginBottom="10dp">
    28  
    29         <TextView
    30             android:layout_width="wrap_content"
    31             android:layout_height="wrap_content"
    32             android:text="年  龄:"
    33             android:textSize="18sp" />
    34  
    35         <EditText
    36             android:id="@+id/et_age"
    37             android:layout_width="match_parent"
    38             android:layout_height="wrap_content"
    39             android:hint="输入年龄"
    40             android:textSize="16sp" />
    41     </LinearLayout>
    42     <LinearLayout
    43         android:layout_width="match_parent"
    44         android:layout_height="wrap_content">
    45         <Button
    46             android:id="@+id/btn_add"
    47             android:layout_width="0dp"
    48             android:layout_height="wrap_content"
    49             android:layout_marginRight="2dp"
    50             android:layout_weight="1"
    51             android:background="#B9B9FF"
    52             android:text="添加"
    53             android:textSize="18sp"
    54             android:onClick="add" />
    55         <Button
    56             android:id="@+id/btn_query"
    57             android:layout_width="0dp"
    58             android:layout_height="wrap_content"
    59             android:layout_marginRight="2dp"
    60             android:layout_weight="1"
    61             android:background="#DCB5FF"
    62             android:text="查询"
    63             android:textSize="18sp"
    64             android:onClick="serch"/>
    65         <Button
    66             android:id="@+id/btn_update"
    67             android:layout_width="0dp"
    68             android:layout_height="wrap_content"
    69             android:layout_marginRight="2dp"
    70             android:layout_weight="1"
    71             android:background="#E6CAFF"
    72             android:text="修改"
    73             android:textSize="18sp"
    74             android:onClick="update"/>
    75         <Button
    76             android:id="@+id/btn_delete"
    77             android:layout_width="0dp"
    78             android:layout_height="wrap_content"
    79             android:layout_weight="1"
    80             android:background="#ACD6FF"
    81             android:text="删除"
    82             android:textSize="18sp"
    83             android:onClick="delete" />
    84     </LinearLayout>
    85     <TextView
    86         android:id="@+id/tv_show"
    87         android:layout_width="match_parent"
    88         android:layout_height="wrap_content"
    89         android:layout_marginTop="25dp"
    90         android:textSize="20sp" />
    91 </LinearLayout>

    StuOpenHelper

     1 package com.example.zsgc;
     2  
     3 import android.content.Context;
     4 import android.database.sqlite.SQLiteDatabase;
     5 import android.database.sqlite.SQLiteOpenHelper;
     6  
     7 import androidx.annotation.Nullable;
     8  
     9 public class StuOpenHelper extends SQLiteOpenHelper {
    10     public StuOpenHelper(@Nullable Context context) {
    11         super(context, "stu.db", null, 1);
    12     }
    13  
    14     @Override
    15     public void onCreate(SQLiteDatabase sqLiteDatabase) {
    16         sqLiteDatabase.execSQL("create table student (id integer primary key autoincrement ,name varchar(20),price integer)");
    17     }
    18  
    19     @Override
    20     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    21  
    22     }
    23 }

    MainActivity

     1 package com.example.aaaapps;
     2  
     3 import androidx.appcompat.app.AppCompatActivity;
     4  
     5 import android.annotation.SuppressLint;
     6 import android.database.Cursor;
     7 import android.database.sqlite.SQLiteDatabase;
     8 import android.os.Bundle;
     9 import android.view.View;
    10 import android.widget.TextView;
    11 import android.widget.Toast;
    12  
    13 public class MainActivity extends AppCompatActivity {
    14  
    15     @Override
    16     protected void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.activity_main);
    19     }
    20  
    21     @SuppressLint("WrongConstant")
    22     public void add(View view){
    23         String name = ((TextView)findViewById(R.id.et_name)).getText().toString();
    24         int age = Integer.parseInt(((TextView)findViewById(R.id.et_age)).getText().toString());
    25         StuOpenHelper stuOpenHelper=new StuOpenHelper(this);
    26         SQLiteDatabase db = stuOpenHelper.getReadableDatabase();
    27         db.execSQL("insert into student (name,price) values(?,?)",new Object[]{name,age});
    28         Toast.makeText(this,"ok",0).show();
    29     }
    30  
    31     public void serch(View view){
    32         StuOpenHelper stuOpenHelper=new StuOpenHelper(this);
    33         SQLiteDatabase db = stuOpenHelper.getReadableDatabase();
    34         String s="";
    35         Cursor cursor = db.rawQuery("SELECT * from student", null);
    36         while (cursor.getCount()!=0){
    37             while (cursor.moveToNext()){
    38                 s+=cursor.getInt(0)+" "+cursor.getInt(1)+" "+cursor.getInt(2)+"
    ";
    39             }
    40         }
    41         ((TextView)(findViewById(R.id.tv_show))).setText(s);
    42     }
    43 }
  • 相关阅读:
    DHCP服务搭建
    JumpServer跳板机
    PXE
    DNS
    MySQL
    企业级LNMP分离式部署
    MHA-Atlas-MySQL高可用集群2
    MHA-Atlas-MySQL高可用集群
    备份全网服务器数据
    FTP
  • 原文地址:https://www.cnblogs.com/baigei/p/14044461.html
Copyright © 2020-2023  润新知