android第六次作业
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@mipmap/back" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginTop="12dp" android:text="姓名" android:textColor="#DE9898" android:id="@+id/tv1"> </TextView> <EditText android:id="@+id/et1" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_toRightOf="@id/tv1"> </EditText> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="年龄" android:textColor="#DE9898" android:id="@+id/tv2" android:layout_below="@id/tv1"> </TextView> <EditText android:id="@+id/et2" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_below="@id/tv1" android:layout_toRightOf="@id/tv2"> </EditText> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:text="注册" android:textColor="#DE9898" android:background="#898888" android:layout_below="@id/tv2" android:layout_marginHorizontal="135dp" android:onClick="click1"> </Button> </RelativeLayout> </androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myhomework5; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Intent; 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); this.setTitle("注册界面"); StudentDB studentDB = new StudentDB(this); studentDB.getWritableDatabase(); } public void click1(View view){ String username = ((EditText)findViewById(R.id.et1)).getText().toString(); String age = ((EditText)findViewById(R.id.et2)).getText().toString(); insert(username,age); Toast.makeText(MainActivity.this,"注册成功",Toast.LENGTH_LONG).show(); } public void insert(String name , String age){ StudentDB studentDB = new StudentDB(this); SQLiteDatabase sqLiteDatabase = studentDB.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("name",name); contentValues.put("age",age); sqLiteDatabase.insert("Stu",null,contentValues); sqLiteDatabase.close(); } } package com.example.myhomework5; import android.content.Context; import android.content.Intent; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class StudentDB extends SQLiteOpenHelper{ public StudentDB(Context context) { super(context, "stu.db", null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { String sql = "CREATE TABLE Stu(id INTEGER PRIMARY KEY autoincrement, NAME VARCHAR(20), age VARCHAR(10))"; sqLiteDatabase.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } } package com.example.myhomework5; import android.content.Context; import android.content.Intent; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class StudentDB extends SQLiteOpenHelper{ public StudentDB(Context context) { super(context, "stu.db", null, 3); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { String sql = "CREATE TABLE Stu(id INTEGER PRIMARY KEY autoincrement, NAME VARCHAR(20), age VARCHAR(10))"; sqLiteDatabase.execSQL(sql); } // 数据库版本更新被调用,版本只能升不能降 @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { String sql2 = "CREATE TABLE bj(id INTEGER PRIMARY KEY autoincrement, Class_NAME VARCHAR(20))"; sqLiteDatabase.execSQL(sql2); } }