SQLite数据库的增删改查(1) 使用sql语句

一、MyOpenhelper 类

package com.hiscene.testsqlite;

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

import androidx.annotation.Nullable;

public class MyOpenhelper extends SQLiteOpenHelper {

    private String TAG="TestSqlite";

    public MyOpenhelper(Context context) {
        super(context, "daliang.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table person(_id integer primary key autoincrement,name text,age integer)");
        Log.d(TAG,"MyOpenhelper/onCreate()");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        Log.d(TAG,"MyOpenhelper/onUpgrade()");
    }
}

 

二、UI搭建

<?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">

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="92dp"
        android:layout_marginLeft="92dp"
        android:layout_marginTop="148dp"
        android:onClick="crud"
        android:text="添加"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="44dp"
        android:layout_marginLeft="44dp"
        android:onClick="crud"
        android:text="删除"
        app:layout_constraintStart_toEndOf="@+id/btnAdd"
        app:layout_constraintTop_toTopOf="@+id/btnAdd" />

    <Button
        android:id="@+id/btnModify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:onClick="crud"
        android:text="修改"
        app:layout_constraintStart_toStartOf="@+id/btnAdd"
        app:layout_constraintTop_toBottomOf="@+id/btnAdd" />

    <Button
        android:id="@+id/btnQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="crud"
        android:text="查询"
        app:layout_constraintBottom_toBottomOf="@+id/btnModify"
        app:layout_constraintEnd_toEndOf="@+id/btnDelete"
        app:layout_constraintStart_toStartOf="@+id/btnDelete"
        app:layout_constraintTop_toTopOf="@+id/btnModify" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

三、具体逻辑(代码如下)

package com.hiscene.testsqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void crud(View view) {

        MyOpenhelper myOpenhelper = new MyOpenhelper(this);
        //没有数据库,者创建数据库
        SQLiteDatabase database = myOpenhelper.getWritableDatabase();

        switch (view.getId()) {
            case R.id.btnAdd:

                //添加一个人物表,名字为张三,年龄为18
                database.execSQL("insert into person(name,age) values(?,?)", new Object[]{"张三", 18});
                break;

            case R.id.btnModify:

                //修改人物表中名字为张三的数据,年龄改成为20
                database.execSQL("update person set age=20 where name=?",new Object[]{"张三"});
                break;

            case R.id.btnDelete:

                //删除人物表中名字为张三的数据
                database.execSQL("delete from person where name=?",new Object[]{"张三"});
                break;

            case R.id.btnQuery:

                //查询人物表中名字为张三的数据
                Cursor cursor =database.rawQuery("select * from person where name=?",new String[]{"张三"});
                while (cursor.moveToNext()){

                    int id=cursor.getInt(0);
                    String name=cursor.getString(1);
                    int age =cursor.getInt(2);
                    System.out.println(String.format("[id=%s,name=%s,age=%s]",id,name,age));
                }

                break;
        }

        database.close();
    }
}

猜你喜欢

转载自blog.csdn.net/a451319296/article/details/110507676