目录

SQLite Debugging Tool

SQLite is a lightweight, embedded, serverless relational database management system. Its core advantage is that the entire database is stored in a single file, without the need for an independent server process. It supports cross-platform operation and is widely used in mobile applications, embedded devices, and desktop software.

This debugging tool implements operations on the SQLite database based on the hdc command. It provides an efficient way to manage the SQLite database through the CLI, which is applicable to quick database operations and verification in the development, debugging, and O&M phases.

  • The SQLite debugging tool is available since HarmonyOS 6.0.0.

  • You can also use DevEco Studio to debug the database. For details, see App Database Access.

Environment Requirements

Preparations

Before using SQLite, switch to the target application path and then run the command to access the SQLite debugging tool.

# Open the hdc CLI.
c:/users/zzz>hdc shell
$ cd /data/app/el1/100/base/com.test.myapplication   // Go to the target application path. (The current path is an example. Use the path based on your actual needs.)
$ ls -lZ                                             // Check the database files in the path. If a file is labeled with debug_hap_data_file, the file is the target application file.
total 9531
drwxrwsr-x 2 20020197 ddms o:object_r:debug_hap_data_file:s0:x229,x334,x512,x868,x1024     3440 2025-08-08 16:54 lock
-rw-rw---- 1 20020197 ddms o:object_r:debug_hap_data_file:s0:x229,x334,x512,x868,x1024  9228288 2025-08-08 16:55 rdbPerfTest.db
-rw-rw---- 1 20020197 ddms o:object_r:debug_hap_data_file:s0:x229,x334,x512,x868,x1024      343 2025-08-08 16:54 rdbPerfTest.db-compare
-rw-rw---- 1 20020197 ddms o:object_r:debug_hap_data_file:s0:x229,x334,x512,x868,x1024    12288 2025-08-08 16:55 rdbPerfTest.db-dwr
-rw-rw---- 1 20020197 ddms o:object_r:debug_hap_data_file:s0:x229,x334,x512,x868,x1024    32768 2025-08-08 16:55 rdbPerfTest.db-shm
-rw-rw---- 1 20020197 ddms o:object_r:debug_hap_data_file:s0:x229,x334,x512,x868,x1024   444992 2025-08-08 16:55 rdbPerfTest.db-wal
$ sqlite3 rdbPerfTest.db                             // Open the database file.
SQLite version 3.44.4 2025-02-19 00:18:53
Enter ".help" for usage hints.
sqlite>

If you create a database and table in the path of a non-target application, the system throws a permission error.

Run the CREATE TABLE command in the SQL statement to create a table.

# Open the hdc CLI.
c:/users/zzz>hdc shell
# Open or create a database.
$ sqlite3 a.db
SQLite version 3.44.4 2025-02-19 00:18:53
Enter ".help" for usage hints.
# Create a table.
sqlite>create table t1(a int);
Error: unable to open database "a.db": unable to open database file
# Open the asd.db database.
sqlite>.open asd.db
Error: unable to open database "asd.db": unable to open database file
Notice: using substitute in-memory database instead of "asd.db"

When the preceding command is run, SQLite attempts to open or create a table and the asd.db database. However, the following error is reported due to insufficient permission:

# Create a table.
sqlite>create table t1(a int);
Error: unable to open database "a.db": unable to open database file
# Open the asd.db database.
sqlite>.open asd.db
Error: unable to open database "asd.db": unable to open database file
Notice: using substitute in-memory database instead of "asd.db"

After SQLite accesses the encryption database, the following error information is displayed:

# sqlite3 ./wallet_personal_info_data_relational_store
SQLite version 3.44.4 2025-02-19 00:18:53
Enter ".help" for usage hints.
sqlite> select * from sqlite_master;
Parse error: file is not a database (26)
sqlite>.table
Error: file is not a database
sqlite>.q

Command List

The following table lists the commands supported by the SQLite debugging tool.

Command Description
.help Displays all available SQLite commands and brief descriptions.
.open database name Opens or creates a database.
.databases Lists all connected databases.
.tables Lists all tables in a database.
.show Displays the default settings of the SQLite command prompt.
.schema Obtains complete information about a table.
.quit or .exit Exits the SQLite CLI.
.schema [TABLE] Displays the SQL statement for creating a table (or the structure of all tables).

Notes

Command Usage and Examples

[h2]Displaying All Commands and Descriptions

sqlite>.help

[h2]Creating or Opening an Existing Database

[h2]Creating a Table

You can run the SQL statement create table to create a COMPANY table, set ID as the primary key, and set NOT NULL to ensure that the fields cannot be empty when records are created in the table.

sqlite>create table COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

[h2]Querying a Table

Run the SQLite command .tables to check whether the table is created. This command is used to list all tables in a database.

sqlite>.tables
COMPANY

After the .tables command is run, the created COMPANY table is listed.

Run the SQLite command .schema to obtain the complete information about the table.

sqlite>.schema COMPANY
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

[h2]Deleting a Table

The SQL statement DROP TABLE is used to delete a table definition and all associated data, indexes, triggers, constraints, and permission specifications.

For example, if the COMPANY table already exists in the database, you can run the following command to delete it:

sqlite>DROP TABLE COMPANY;

After the command is run, the COMPANY table cannot be queried using the .tables command. If the command output is empty, the table is deleted successfully.

sqlite>.tables

[h2]Inserting Data

[h2]Querying Data

Run the sqlite> SELECT command in either of the following manners to query data:

[h2]Updating Data

At the sqlite> prompt, run the UPDATE table name SET age = 31 WHERE name = 'Zhang San'; SQL statement to update the data.

sqlite>UPDATE COMPANY SET age = 31 WHERE name = 'Zhang San';
sqlite>SELECT * FROM COMPANY;
1|Zhang San|31|Chaoyang District, Beijing|20000.5
3|Wang Wu|25|Tianhe District, Guangzhou|18000.75
4|Zhao Liu|40|Nanshan District, Shenzhen|30000.25

[h2]Deleting Data

At the sqlite> prompt, run the DELETE FROM table name WHERE name = 'Wang Wu'; SQL statement to delete the data.

sqlite> DELETE FROM COMPANY WHERE name = 'Wang Wu';
sqlite> SELECT * FROM COMPANY;
1|Zhang San|31|Chaoyang District, Beijing|20000.5
4|Zhao Liu|40|Nanshan District, Shenzhen|30000.25

References

The SQLite website provides rich sample code, including creating a database, creating a table, inserting data, and querying data.

FAQs

[h2]What should I do if the query result is empty?

In actual database operations, the query result may be empty because no data exists in the table.

To avoid this, you should ensure that related data is inserted to the table before running the query command.

[h2]How do I delete characters?

You can press Ctrl+Backspace to delete a single character and press Ctrl+U to delete all characters.