Android SQLite database addition, deletion, modification and query implementation code and method of computer viewing SQLite database

overview

SQLite has the advantages of being lightweight, independent, and easy to maintain. It supports most of the SQL syntax and is a common method for data storage in Android programming. This article lists common operations such as adding, deleting, and modifying.

Instantiate SQLiteDatabase

First, you need to instantiate a SQLiteDatabase, and then perform other operations. The instantiation code is as follows:

private SQLiteDatabase mDbDialog;

Then make it point to a specific database in the main function;

mDbDialog = openOrCreateDatabase("GIMChat.db", Context.MODE_PRIVATE, null);

Note that the database must be closed after all operations (addition, deletion, modification, etc.) are completed:

mDbExistDialog.close();

add record

Without further ado, let’s start with the code:

mDbDialog.execSQL("CREATE TABLE IF NOT EXISTS dialog (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR, friendname VARCHAR, mecessage VARCHAR, ismesend SMALLINT)");
mDbDialog.execSQL("INSERT INTO dialog VALUES (NULL, ?, ?, ?, ?)",new Object[]{
    
    LoginActivity.Username, friendname, list.get(i).getBody(), 1});

Note:
1. The function of the first sentence of code is: if the table exists in the database, add a record to the table, otherwise, create a table first; 2.
When inserting a record, pay attention to the data type of each field Be consistent with the data type defined in the first sentence.

Delete Record

mDbChat.execSQL("DELETE FROM dialog WHERE friendname = ?", new String[]{
    
    ChatLists.get(position).getFriendname()});

Among them, dialog is the name of the table to be operated on, and friendname is a field in the table (that is, the basis for deleting records).

Query the records

Here is an example of traversing all the records in the query database table (such as retrieving chat records).
First you need to instantiate a database pointer:

private Cursor ExistDialog_cursor;

Then use that pointer in the main function:

mDbExistDialog = openOrCreateDatabase("GIMChat.db", Context.MODE_PRIVATE, null);
        mDbExistDialog.execSQL("CREATE TABLE IF NOT EXISTS dialog (_id INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR, friendname VARCHAR, mecessage VARCHAR, ismesend SMALLINT)");
        ExistDialog_cursor = mDbExistDialog.rawQuery("SELECT * FROM dialog WHERE username == ?", new String[]{
    
    LoginActivity.Username});

By calling the ".moveToNext(
)" method, this pointer can realize the traversal of all records that meet the retrieval conditions. Take the restoration of chat records as an example. The chat records are stored locally on the mobile phone. When the user clicks on the chat box, the corresponding chat records can be restored by searching the "Friendname" and "Minename" fields:

while (ExistDialog_cursor.moveToNext()){
    
    
                dialog.setUsername(ExistDialog_cursor.getString(ExistDialog_cursor.getColumnIndex("username")));
                dialog.setFriendname(ExistDialog_cursor.getString(ExistDialog_cursor.getColumnIndex("friendname")));
                dialog.setMecessage(ExistDialog_cursor.getString(ExistDialog_cursor.getColumnIndex("mecessage")));
                dialog.setisMeeSend(ExistDialog_cursor.getInt(ExistDialog_cursor.getColumnIndex("ismesend")));
                if (dialog.getUsername().equals(LoginActivity.Username)){
    
    
                    if (dialog.getFriendname().equals(ChatActivity.exist_friendname) ||dialog.getFriendname().equals(ChatActivity.exist_friendname+"@47.96.151.84")){
    
    
                        if (dialog.getisMesend() == 0){
    
    
                            ExistDialogManage(false, true, dialog.getMecessage(), ChatActivity.exist_friendname);
                        }else {
    
    
                            ExistDialogManage(false, false, dialog.getMecessage(), ChatActivity.exist_friendname);
                        }
                    }
                }
        }

Note that the pointer should be closed after all operations are done:

ExistDialog_cursor.close();

View SQLite database on computer

In the actual development process, it is often necessary to check the contents of the database to perform operations such as troubleshooting some bugs. The author personally recommends a tool that I personally find useful: SQLite Studio. This software is an open source software, which can view the SQLite database in the Android device on the computer.
——————————————————————————————
Finally, post my personal public account: WeChat search "Chaqian" or scan the picture below. Usually, some programming-related articles will be updated, and everyone is welcome to pay attention~
tea move

Guess you like

Origin blog.csdn.net/weixin_46269688/article/details/110368189