Android笔记--Android+数据库的增加数据的实现
阅读原文时间:2023年07月08日阅读:2

具体实现

添加成功:

界面代码很简单,直接忽略。

连接数据库的代码:

package com.example.myapplication.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Connect extends SQLiteOpenHelper {

    //数据库名
    private static final String db_name="user.db";

    //表名
    private static final String table_name="user_info";

    //版本号
    private static final int db_version=1;

    //数据库帮助器的初定义
    private static Connect mHelper=null;

    //读写得初定义
    private SQLiteDatabase mRDB=null;
    private SQLiteDatabase mWDB=null;

    private Connect(Context context){
        super(context,db_name,null,db_version);

    }

    //利用单例模式获取数据库帮助器的唯一实例
    public static Connect getInstance(Context context){
        if(mHelper==null){
            mHelper=new Connect(context);
        }

        return mHelper;
    }

    //打开数据库得读操作
    public SQLiteDatabase openReadLink(){
        if(mRDB==null||!mRDB.isOpen()){
            mRDB=mHelper.getReadableDatabase();
        }

        return mRDB;
    }

    //打开数据库的写操作
    public SQLiteDatabase openWriteLink(){
        if(mWDB==null||!mWDB.isOpen()){
            mWDB=mHelper.getWritableDatabase();
        }

        return mWDB;
    }

    //关闭数据库连接
    public void closeLink(){
        if(mRDB!=null&&mRDB.isOpen()){
            mRDB.close();
            mRDB=null;
        }

        if(mWDB!=null&&mWDB.isOpen()){
            mWDB.close();
            mWDB=null;
        }
    }

    //创建数据库,执行建表语句
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String sql="create table if not exists "+table_name+" ("+
                " name varchar primary key not null," +
                " cla varchar not null,"+
                " major varchar not null);";

        sqLiteDatabase.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        //onCreate(sqLiteDatabase);
    }

    //添加语句
    public long insert(User user){
        ContentValues values=new ContentValues();
        values.put("name",user.getName());
        values.put("cla",user.getCla());
        values.put("major",user.getMajor());
        //values.put("student",user.getStudent());

        return mWDB.insert(table_name,null,values);
    }
}



package com.example.myapplication;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.database.Connect;
import com.example.myapplication.database.User;

import java.util.Random;

public class start extends AppCompatActivity implements View.OnClickListener {

    private EditText et_name;
    private EditText et_class;
    private EditText et_major;
    private CheckBox cb_remember;

    private Button btn_add;
    private Button btn_delete;
    private Button btn_update;
    private Button btn_check;

    private Connect mHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);

        et_name=findViewById(R.id.et_name);
        et_class=findViewById(R.id.et_class);
        et_major=findViewById(R.id.et_major);
        cb_remember=findViewById(R.id.cb_remember);

        btn_add=findViewById(R.id.btn_add);
        btn_delete=findViewById(R.id.btn_delete);
        btn_update=findViewById(R.id.btn_update);
        btn_check=findViewById(R.id.btn_check);

        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_update.setOnClickListener(this);
        btn_check.setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        super.onStart();

        //获取数据库帮助器实例
        mHelper= Connect.getInstance(this);

        //读写操作
        mHelper.openReadLink();
       SQLiteDatabase db= mHelper.openWriteLink();

        mHelper.onCreate(db);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //关闭操作
        mHelper.closeLink();
    }

    @Override
    public void onClick(View view) {
        String name=et_name.getText().toString();
        String cla=et_class.getText().toString();
        String major=et_major.getText().toString();

        User user=null;

        switch(view.getId()){
            case R.id.btn_add:
                user=new User(name,cla,major);
                //mHelper.insert(user);
                if(mHelper.insert(user)>0){
                    Toast.makeText(this,"添加成功!",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}