Android Sqlite数据库中基础的增删改查操作

2021/4/16 2:28:54

本文主要是介绍Android Sqlite数据库中基础的增删改查操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.创建一个类MyUserlogDBHelper去继承SQLiteOpenHelper,在该类里面创建表。

package com.example.demo;

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

import androidx.annotation.Nullable;

public class MyUserlogDBHelper extends SQLiteOpenHelper {

    public MyUserlogDBHelper db;
    //对外提供参数,单例模式
    private static SQLiteOpenHelper mHelper;
    public static synchronized SQLiteOpenHelper getInstance(Context context){
        if (mHelper==null){
            mHelper=new MyUserlogDBHelper(context,"user_log",null,1);
        }
        return mHelper;
    }

    private MyUserlogDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建表
        String sql="create table user_log (_id integer primary key autoincrement,name text)";
        //拿到db执行sql语句即可
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

2.在Activity中写查询,增加,修改,删除数据的操作。

package com.example.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
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 create(View view) {
        SQLiteOpenHelper helper = MyUserlogDBHelper.getInstance(this);
        SQLiteDatabase readableDatabase = helper.getReadableDatabase();
    }
    //查询语句
    public void query(View view) {
        SQLiteOpenHelper instance = MyUserlogDBHelper.getInstance(this);
        SQLiteDatabase db = instance.getReadableDatabase();

        if (db.isOpen()){
            //查询语句
            String sql="select * from user_log";
            //获取游标
            Cursor cursor = db.rawQuery(sql, null);
            while(cursor.moveToNext()){
                //不规范的写法
                /*int _id=cursor.getColumnIndex("0");
                int name=cursor.getColumnIndex("1");
                */
                //规范的写法
                int _id=cursor.getInt(cursor.getColumnIndex("_id"));
    	        String name=cursor.getString(cursor.getColumnIndex("name"));

                Log.d("数据库", "_id: "+_id+",name:"+name);
            }

            //关闭游标
            cursor.close();
            //关闭数据库
            db.close();
        }
    }

    //插入语句
    public void insert(View view) {
        SQLiteOpenHelper instance = MyUserlogDBHelper.getInstance(this);
        SQLiteDatabase db = instance.getWritableDatabase();

        //判断数据库是否开启
        if (db.isOpen()){
            String sql="insert into user_log(name) values('路宇')";

            db.execSQL(sql);
        }
        db.close();
    }

    //修改
    public void update(View view) {
        SQLiteOpenHelper instance = MyUserlogDBHelper.getInstance(this);
        SQLiteDatabase db = instance.getWritableDatabase();
        if (db.isOpen()){
            String sql="update user_log set name=? where _id=?";
            db.execSQL(sql,new Object[]{"成龙","5"});
        }
        //关闭数据库
        db.close();
    }
    //删除数据
    public void delete(View view) {
        SQLiteOpenHelper instance = MyUserlogDBHelper.getInstance(this);
        SQLiteDatabase db = instance.getWritableDatabase();
        //判断数据是否是打开状态
        if (db.isOpen()){
            String sql="delete from user_log where _id=?";
            db.execSQL(sql,new Object[]{6});
        }
    }
}

3.布局文件。

<?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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="创建表"
        android:onClick="create" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:onClick="query" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="插入"
        android:onClick="insert" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改"
        android:onClick="update" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"
        android:onClick="delete" />



</LinearLayout>

以上就是SQLite数据库中,最简单的增删改查操作,想深入学习SQLite在项目中的实际应用,请持续关注后面博文!



这篇关于Android Sqlite数据库中基础的增删改查操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程