SQLite database and its basic use in Android development

SQLite database and its basic use in Android development

Features of SQLite database

Lightweight database, no need to build a server.

SQLite environment configuration

Here is the method to download from the official website. If you have an Android Studio project, you can jump to this article: Application of SQLite in Android .

1. Enter the sqlite official website (https://www.sqlite.org/index.html), enter Download
insert image description here

2. Select sqlite-tools under the Windows version and download the compressed package
insert image description here

3. Unzip the file
insert image description here

4. Enter the system environment configuration interface (can be searched in the settings), edit environment variables
insert image description here

5. Add the path of sqlite3.exe to the Path of the system variable (such as C:\Program Files (x86)\sqlite-tools-win32-x86-3360000)
insert image description here

6. Enter the command line, enter the sqlite3 statement, and test whether the environment is successfully installed
insert image description here

Common basic operations of SQLite

view database

.database

open database

.open 数据库文件名(后缀名为.db)

insert image description here

Show create operations in this database

.schema 

insert image description here

view data sheet

.table

Create data table (same as SQL language)

create table 表名(字段1 数据类型, 字段2 数据类型, ...);

insert image description here

SQL language

SQL language can be used here, such as insert (), select, update ()
insert image description here

Application of SQLite in Android

Find sqlite3.exe

1. Open Android Studio, find SDK Manager on the upper right, open and copy the SDK path
insert image description here

2. In the Sdk/platform-tools directory, you can find the running file sqlite3.exe of the SQLite database, and then configure the environment variables as the above steps.
insert image description here

access database

1. Create a new public class inherited from SQLiteOpenHelper. After it is built, you need to rewrite the constructor, as well as the two methods onCreate() and onUpgrade()

public class MyOpenHelper extends SQLiteOpenHelper {
    
    
    //重写构造函数
    public MyOpenHelper(Context context){
    
    
        super(context, "taskDB.db", null,1);
        //taskDB.db为数据库名,若不存在则创建
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    
    
        String create_sql = 
            "create table task(id INTEGER PRIMARY KEY AUTOINCREMENT, content varchar(50), status int);";
        db.execSQL(create_sql);  //执行SQL语句
    }

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

    }
}

modify or insert into the database

//新建MyOpenHelper实例,注意自己重新定义的构造函数的参数
MyOpenHelper openHelper = new MyOpenHelper(this);
//通过getWritableDatabase()创建用于写数据库的实例
SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();

//新建ContentValues类,用来存放插入数据库的数据
ContentValues task_item = new ContentValues();
//ContentValues.put(key, value)  key需要与数据表的字段名对应
task_item.put("content", ((EditText) findViewById(R.id.Content)).getText().toString());
task_item.put("status",0);

//执行插入语句 writableDatabase.insert(数据表名, nullColumnHack, ContentValues)
//nullColumnHack通常为null
writableDatabase.insert("task", null, task_item);
//关闭数据库,减少内存资源浪费
writableDatabase.close();

read database

public List<TaskItem> task_list = new ArrayList<>();

public void ReadDataBase(){
    
    
    MyOpenHelper openHelper = new MyOpenHelper(this);
    //创建SQLiteDatabase实例并获取ReadableDatabase
    SQLiteDatabase read_db = openHelper.getReadableDatabase();
    //使用ReadableDatabase中的游标
    Cursor cursor = read_db.query("task",new String[]{
    
    "id", "content", "status"},null,null,null,null,null);
    //使用while语句遍历游标Cursor,将从数据库获取的数据放进list列表中
    while(cursor.moveToNext()){
    
    
        TaskItem task_item = new TaskItem();
        //游标get方法传入的字段索引值是从0开始的
        task_item.setId(cursor.getInt(0));
        task_item.setContent(cursor.getString(1));
        task_item.setStatus(cursor.getInt(2));
        task_list.add(task_item);
    }

    //关闭游标和ReadableDatabase,减少内存资源浪费
    cursor.close();     
    read_db.close();   
}

class TaskItem {
    
    
    private int id;
    private String content;
    private int status;

    public int getId() {
    
    
        return id;
    }
    public String getContent() {
    
    
        return content;
    }
    public int getStatus() {
    
    
        return status;
    }
    public void setId(int id) {
    
    
        this.id = id;
    }
    public void setContent(String content) {
    
    
        this.content = content;
    }
    public void setStatus(int status) {
    
    
        this.status = status;
    }
}

Guess you like

Origin blog.csdn.net/Dae_Lzh/article/details/121139948