I. Overview
This is to implement the registration function and store the registered information in the database.
Some simple understanding of SQLite
SQLite is a lightweight relational database that has fast computing speed and takes up few resources. Features : lightweight, independence, isolation, cross-platform, security, multi-language interface
2. Page display
【figure 1】
3. Code part
1. Registered layout activity_regiter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Register">
<!-- 外部-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textVeiw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="帐号注册信息页面"
android:layout_gravity="center_vertical|center_horizontal"
android:textSize="30dp"
android:layout_margin="30dp"
android:paddingBottom="20dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="5dp">
<TextView
android:id="@+id/textView1"
android:layout_width="71dp"
android:layout_height="25dp"
android:text="帐号:" />
<EditText
android:id="@+id/et_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center">
<TextView
android:id="@+id/textView2"
android:layout_width="71dp"
android:layout_height="25dp"
android:text="密码:" />
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center">
<TextView
android:id="@+id/textView3"
android:layout_width="71dp"
android:layout_height="25dp"
android:text="确认密码:" />
<EditText
android:id="@+id/et_repassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView4"
android:layout_width="71dp"
android:layout_height="25dp"
android:text="姓名:" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView5"
android:layout_width="71dp"
android:layout_height="25dp"
android:text="手机号:" />
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交"
android:layout_margin="20dp"/>
<Button
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
2. Create a new package, name: Bean; create a Userinfo class (registration class) in this directory
Create a new package named: Bean; create a Userinfo class in this directory
[Figure: Directory structure diagram]
Userinfo class code:
package com.sziiit.cgy_620_sql.Bean;
public class Userinfo {
private String account, password, name, phone;
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3. Create a db package and create a MySqliteOpenHpler class in it
SQLite related functions:
MYSQLiteOpenHelper - Create a new database
onCreate(SQLiteDatabase db)——Create a new database table
onUpgrade——Upgrade use of database
1. Use the SQL statement CREATE_USER to create the user table
2. The MySqliteOpenHelper class should inherit the parent class SQLiteOpenHelper, implement the constructor MySQLiteOpenHelper(), and assign global variables to mContext
3. Create a data table through SQL statements, use sqliteDatabase.execSQL (CTREATE_USER) - create a user table (registry table)
MySqliteOpenHpler class code:
package com.sziiit.cgy_620_sql.db;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public static final String CREATE_USER = "create table user(" +
"id integer primary key autoincrement," +
"account text," +
"password text," +
"name text," +
"phone integer)";
private Context mContext;
public MySQLiteOpenHelper(@Nullable Context context) {
super(context, "user.db",null,1);
this.mContext = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_USER);
Toast.makeText(mContext,"创建表成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
4. RegiserActivity class
aaaaaaa