安卓数据库
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="130dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓 名 :" android:textSize="18sp" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入姓名" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="年 龄:" android:textSize="18sp" /> <EditText android:id="@+id/et_age" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入年龄" android:textSize="16sp" /> </LinearLayout> <Button android:id="@+id/btn_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_age" android:layout_marginRight="2dp" android:background="#B9B9FF" android:onClick="add" android:text="注册" android:textSize="18sp" /> <TextView android:id="@+id/tv_show" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="25dp" android:textSize="20sp" /> </LinearLayout>
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void add(View v) { StuOpenHelper helper = new StuOpenHelper(this); SQLiteDatabase db = helper.getWritableDatabase(); String name = ((EditText) findViewById(R.id.et_name)).getText() .toString(); int age = Integer.parseInt(((EditText) findViewById(R.id.et_age)) .getText().toString()); db.execSQL("insert into stuinfo (name,age) values(?,?)", new Object[]{ name, age}); Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show(); } }
package com.example.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class StuOpenHelper extends SQLiteOpenHelper { public StuOpenHelper(Context context) { super(context, "student.db", null, 2); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table stuinfo(_id integer primary key autoincrement,name varchar(20),age integer)"); db.execSQL("create table class(_id integer primary key autoincrement,classname varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { System.out.println("版本更新,老版本是"+ i+"新版本是"+i1); } }