• Android学习笔记 45. SQLite


    Android学习笔记

    Android基础开发——数据存储

    45. SQLite

    45.1 SQLite介绍

    SQLite关系型数据库。

    嵌入式的数据库,体积小,功能强大,几十kb。

    在Android平台上,集成了一个嵌入式关系型数据库—SQLite,SQLite3支持NULL、INTEGER、REAL(浮点数字)、TEXT(字符串文本)和BLOB(二进制对象)数据类型,虽然它支持的类型只有五种,但实际上sqlite3也接受varchar(n)、char(n)、decimal(p,s)
    等数据类型,只不过在运算或保存时会转成对应的五种数据类型。SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,但是主键只能是Integer类型的。
    Sqlite数据库一般要求主键是_id,当然也可以是id.
    原来:数据库
    安装一个数据库的软件。
    android里面的数据库是由底层的sqilte.c的代码来动态生成的。

    45.2 SQLite可视化工具

    笔者没用老师用的,我直接用Navicat 15

    在这里插入图片描述

    45.3 SQLite创库创表

    Android系统封装了一个类 SqliteOpenHelper

    新建工程

    在这里插入图片描述

    自定义类

    package com.dingjiaxiong.mysqlite;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    import androidx.annotation.Nullable;
    
    public class MySQLiteOpenHelper extends SQLiteOpenHelper {
        
        private static SQLiteOpenHelper mInstance;
        public static synchronized SQLiteOpenHelper getmInstance(Context context){
            if(mInstance == null){
                mInstance = new MySQLiteOpenHelper(context , "dingjiaxiong.db",null,1);
            }
            return mInstance;
        }
        
        private MySQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
        }
    
        //数据库初始化用的
        @Override
        public void onCreate(SQLiteDatabase db) {
            
        }
    
        //数据库升级用
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        }
        
    }
    
    • 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

    布局

    
    <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="生成DB文件"
            android:onClick="createDB"
            />
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    运行

    在这里插入图片描述

    在这里插入图片描述

    这个表是默认生成的,无用。(意思是英文环境)

    创建表

    在这里插入图片描述

    运行程序

    在这里插入图片描述

    45.4 SQLite增删改查

    修改布局

    
    <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="生成DB文件"
            android:onClick="createDB"
            />
    
        <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>
    
    • 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
    package com.dingjiaxiong.mysqlite;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.annotation.SuppressLint;
    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 createDB(View view) {
            SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
    
            //database文件夹 → 创建
            SQLiteDatabase readableDatabase = helper.getReadableDatabase();
        }
    
        public void query(View view) {
            SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
            SQLiteDatabase db = helper.getReadableDatabase();
    
            if (db.isOpen()) {
                Cursor cursor = db.rawQuery("select * from persons", null);
                //迭代游标
                while (cursor.moveToNext()) {
    //                int _id = cursor.getInt(0);
    //                @SuppressLint("Range") int _id = cursor.getInt(cursor.getColumnIndex("_id"));
    //                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
    
                    int _id = cursor.getInt(0);
                    String name = cursor.getString(1);
    
                    Log.e("dingjiaxiong", "query: _id: " + _id + " name: " + name);
    
                }
    
                cursor.close();
                db.close();
            }
        }
    
        public void insert(View view) {
            SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
            SQLiteDatabase db = helper.getWritableDatabase();
    
            if (db.isOpen()) {
                String sql = "insert into persons(name) values('dingjiaxiong')";
                db.execSQL(sql);
                db.close();
            }
            
    
        }
    
        public void update(View view) {
            SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
            SQLiteDatabase db = helper.getWritableDatabase();
    
            if (db.isOpen()) {
                String sql = "update persons set name = ? where _id = ?";
                db.execSQL(sql,new Object[]{"凤凤",4});
                db.close();
            }
            
        }
    
        public void delete(View view) {
            SQLiteOpenHelper helper = MySQLiteOpenHelper.getmInstance(this);
            SQLiteDatabase db = helper.getWritableDatabase();
    
            if (db.isOpen()) {
                String sql = "delete from persons where _id = ?";
                db.execSQL(sql,new Object[]{6});
                db.close();
            }
            
        }
    }
    
    • 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

    运行

    在这里插入图片描述

  • 相关阅读:
    TF-IDF、BM25传统算法总结
    flink-sql读写hive
    昇思25天学习打卡营第01天|imdeity
    利用星穹云Serverless云平台高效开发小程序的技术实践
    icon免费网址
    LeetCode_二叉搜索树_中等_449.序列化和反序列化二叉搜索树
    LeetCode 1710. 卡车上的最大单元数
    Springboot之Jasypt配置文件加密/解密【源码分析】
    day60
    共谋工业3D视觉发展,深眸科技以自研解决方案拓宽场景应用边界
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/126314404