今天看啥  ›  专栏  ›  qq_45973003

android的SQLite数据库操作

qq_45973003  · CSDN  ·  · 2020-11-10 14:15

我们的android布局页面:
在这里插入图片描述
布局部分的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <!--使用的就是我们的啊相对布局的方式来布局页面-->
    <EditText
        android:id="@+id/edit1"
        android:hint="请输入你要插入的数据"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <!--接下来是跟新前后的数据的编写-->
    <EditText
        android:layout_below="@+id/edit1"
        android:id="@+id/edit2left"
        android:hint="更新前的数据"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <EditText
        android:layout_below="@+id/edit1"
        android:layout_toRightOf="@id/edit2left"
        android:id="@+id/edit2right"
        android:hint="更新后的数据"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/del"
        android:layout_width="236dp"
        android:layout_height="56dp"
        android:layout_below="@id/edit2right"
        android:hint="删除后的数据" />

    <!--接下来就是控制我们的按钮-->
    <Button
        android:id="@+id/insert"
        android:text="插入数据"
        android:layout_toRightOf="@id/edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn_del"
        android:text="删除"
        android:layout_toRightOf="@id/insert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/update"
        android:text="更新数据"
        android:layout_toRightOf="@id/edit2right"
        android:layout_below="@id/edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn_del2"
        android:text="删除"
        android:layout_toRightOf="@id/update"
        android:layout_below="@id/edit1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn_delete"
        android:text="执行删除"
        android:layout_below="@id/edit2right"
        android:layout_toRightOf="@id/del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn_del3"
        android:text="删除"
        android:layout_toRightOf="@id/update"
        android:layout_below="@id/edit2right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <!--最后的两个按钮查询所有和清除所有-->
    <Button
        android:id="@+id/find_all"
        android:text="查询所有"
        android:layout_below="@id/del"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/delete_all"
        android:text="清除所有"
        android:layout_below="@id/find_all"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/text_view"
        android:layout_below="@id/delete_all"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>
</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98

然后就是我们的一个就是的MyHelper类
的代码如下:

package com.example.android_testanddenglu;

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

import androidx.annotation.Nullable;

public class MyHelper extends SQLiteOpenHelper {
    //这个是包含所有参数的构造方法
    public MyHelper(@Nullable Context context) {
        super(context, "database", null, 1);
    }
    //这个是创建数据库的方法
    @Override
    public void onCreate(SQLiteDatabase db) {
         //创建数据库的sql语句
        db.execSQL("CREATE TABLE information(id varchar(20),name varchar(20))");
    }
    //这个是版本更新的方法
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

然后是我们的就是Mainactivity的作用:

package com.example.android_testanddenglu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MyHelper myHelper;
    private EditText edit1;
    private EditText edit2left;
    private EditText del;
    private Button insert;
    private Button update;
    private Button find_all;
    private Button btn_delete;
    private String id;
    private String name;
    private SQLiteDatabase db;
    private TextView text_view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建了一个帮助类的对象
        myHelper = new MyHelper(this);
        //调用了一个方法
        init();
    }

    private void init() {
        edit1 = (EditText) findViewById(R.id.edit1);
        edit2left = (EditText) findViewById(R.id.edit2left);
        del = (EditText) findViewById(R.id.del);
        text_view = (TextView) findViewById(R.id.text_view);
        insert = (Button) findViewById(R.id.insert);
        update = (Button) findViewById(R.id.update);
        find_all = (Button) findViewById(R.id.find_all);
        btn_delete = (Button) findViewById(R.id.btn_delete);

        //设置点击监听事件
        insert.setOnClickListener(this);
        update.setOnClickListener(this);
        find_all.setOnClickListener(this);
        btn_delete.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.insert:
                //就是插入数据
                //1:获取到我们用户的数据
                id = edit1.getText().toString();
                name = edit2left.getText().toString();
                //2: 创建我们的就是Soliteopenhelper对象将它转换为我们的成员变量,然后就是获取到一个只读的数据库对象
                db = myHelper.getReadableDatabase();
                //3:创建一个我们存储数据的content对象
                ContentValues values = new ContentValues();
                //这里我们是以键值对的方式来存储数据的
                values.put("id",id);
                values.put("name", name);
                //然后将我们的数据插入进去
                db.insert("information",null,values);
                //结束后关闭数据库
                db.close();
                break;
            case R.id.update:
                //更新数据
                db = myHelper.getWritableDatabase();
                //然后就是创建一个存储数据的对象
                values = new ContentValues();
                //然后就是传入将要跟新的数据
                values.put("name", name = edit2left.getText().toString() );
                db.update("information", values, "id= ? ", new String[]{ edit1.getText().toString()} );
                db.close();
                break;
            case R.id.find_all:
                //查询数据的话就要创建一个就是我们的啊游标
                db = myHelper.getReadableDatabase();
                Cursor cursor = db.query("information", null,null,null,null,null,null);
                while (cursor.moveToNext()){
                    text_view.setText("\n"+"id"+cursor.getString(0)+"name"+cursor.getString(1));
                }
                cursor.close();
                db.close();
                break;
            case R.id.btn_delete:
                //删除数据
                db = myHelper.getWritableDatabase();
                db.delete("information",null,null);
                Toast.makeText(this,"数据已经删除",Toast.LENGTH_SHORT).show();
                text_view.setText("");
                db.close();
                break;

        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112

在这里插入图片描述




原文地址:访问原文地址
快照地址: 访问文章快照