1建立数据库
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:orientation="vertical" 6 android:padding="16dp" > 7 8 9 <LinearLayout 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:layout_marginTop="130dp" > 13 14 <TextView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="姓 名 :" 18 android:textSize="18sp" /> 19 20 <EditText 21 android:id="@+id/et_name" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" 24 android:hint="请输入姓名" 25 android:textSize="16sp" /> 26 </LinearLayout> 27 28 29 <LinearLayout 30 android:layout_width="match_parent" 31 android:layout_height="wrap_content" 32 android:layout_marginBottom="10dp" > 33 34 <TextView 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="年 龄:" 38 android:textSize="18sp" /> 39 40 <EditText 41 android:id="@+id/et_age" 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:hint="输入年龄" 45 android:textSize="16sp" /> 46 </LinearLayout> 47 48 49 <Button 50 android:id="@+id/btn_add" 51 android:layout_width="wrap_content" 52 android:layout_height="wrap_content" 53 android:layout_below="@+id/et_age" 54 android:layout_marginRight="2dp" 55 android:background="#B9B9FF" 56 android:onClick="add" 57 android:text="注册" 58 android:textSize="18sp" /> 59 60 <TextView 61 android:id="@+id/tv_show" 62 android:layout_width="match_parent" 63 android:layout_height="wrap_content" 64 android:layout_marginTop="25dp" 65 android:textSize="20sp" /> 66 67 </LinearLayout>
1 package com.example.myapplication; 2 3 import androidx.appcompat.app.AppCompatActivity; 4 5 import android.database.sqlite.SQLiteDatabase; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 public class MainActivity extends AppCompatActivity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 } 19 20 public void add(View v) { 21 StuOpenHelper helper = new StuOpenHelper(this); 22 SQLiteDatabase db = helper.getWritableDatabase(); 23 String name = ((EditText) findViewById(R.id.et_name)).getText() 24 .toString(); 25 int age = Integer.parseInt(((EditText) findViewById(R.id.et_age)) 26 .getText().toString()); 27 28 db.execSQL("insert into stuinfo (name,age) values(?,?)", new Object[]{ 29 name, age}); 30 Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show(); 31 32 } 33 }
1 package com.example.myapplication; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 7 8 9 public class StuOpenHelper extends SQLiteOpenHelper { 10 11 public StuOpenHelper(Context context) { 12 super(context, "student.db", null, 2); 13 } 14 15 @Override 16 public void onCreate(SQLiteDatabase db) { 17 db.execSQL("create table stuinfo(_id integer primary key autoincrement,name varchar(20),age integer)"); 18 db.execSQL("create table class(_id integer primary key autoincrement,classname varchar(20))"); 19 } 20 21 @Override 22 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 23 System.out.println("版本更新,老版本是"+ i+"新版本是"+i1); 24 25 } 26 }