SQLite database management commands
1. Create a database
Start the command line and run the following command to open the shell mode CLP:
Sqlite3 test. DB
Although we provide the database name, if the database does not exist, SQLite does not actually create the database until it creates some content in the database.
2. Create a data table
SQLite> Create Table member (ID integer primary key, Name text, age integer, ADDR text );
Note: ID is the primary key, and This column has the property of automatic growth by default.
3. insert data
SQLite> insert into Member values (0, 'wwl', 21, 'shanghai'); // The column id = 0 must not exist; otherwise, an error occurs.
Or SQLite> insert into member (name, age, ADDR) values ('wwl', 21, 'shanghai ');
3. query data
SQLite>. Mode Column
SQLite>. headers on
SQLite> select * from member;
Note: The two commands before the SELECT statement (. headers and. mode) are used to improve the display format.
4. Create views and Indexes
SQLite> Create view schema as select * from member;
SQLite> Create index member_idx on member (ID)
5. Export data
You can use the. Dump command to export database objects to the SQL format. Without any parameters,. Dump exports the entire database as a database Definition Language (DDL) or database operation language (DML) command. It is suitable for recreating database objects and data in the database. If a parameter is provided, shell parses the parameter as the table name or view and exports any tables or views that match the given parameter. Otherwise, those that do not match will be ignored.
By default, the output of the. Dump command is directed to the screen. For example,. Dump
To redirect the output to a file, run the. Dump [filename] command to redirect all the output to the specified file. To restore the output to the screen, run the. Output stdout command.
SQLite>. output file. SQL
SQLite>. Dump
SQLite>. Output stdout
NOTE: If file. SQL does not exist, the file will be created in the current working directory. If the file exists, it will be overwritten.
6. Import Data
There are two ways to import data, depending on the file format to be imported. If the file is composed of SQL statements, you can use the. read command to import the commands contained in the file. If the file contains values separated by commas (,) or other delimiters (comma-swparated values, CSV), you can use. import [Table] command, which parses the specified file and tries to insert data into the specified table.
The. read command is used to import files created by the. Dump command. If you use the file. SQL exported as a backup file, you must first remove the existing database object and then re-import it using the following method:
SQLite> drop table member;
SQLite> drop view schema;
SQLite>. Read File. SQL
7. Back up the database
There are two ways to back up the database, depending on the type of backup you want. SQL dumping is the best portable backup.
The standard way to generate a dump is to use the CLP. Dump command: sqlite3 test. DB. Dump> test. SQL
In shell, you can redirect the output to an external file and execute the command to restore it to the screen output, for example:
SQLite>. output file. SQL
SQLite>. Dump
SQLite>. Output stdout
SQLite>. Exit
Similarly, it is easy to use SQL dump as the CLP input stream to import databases:
Sqlite3 test. DB <test. SQL
The knowledge of backing up a binary database is a little bit more work than copying files. You need to clear the database before the backup, so that you can release some deleted objects that are no longer in use. This database file will become smaller, so the binary copy will be smaller:
Sqlite3 test. DB vacuum
CP test. DB test. Backup
8. Other commands
SQLite> select last_insert_rowid (); // obtain the auto-increment value of the last insert.
SQLite>. Tabes // return all tables and views
SQLite>. Indices member // view the index of a table
SQLite>. schema member // get a table or view definition (DDL) statement. If the table name is not provided, return the Definition Statement of All database objects (table, view, index, triger ).
1. Create a database
Start the command line and run the following command to open the shell mode CLP:
Sqlite3 test. DB
Although we provide the database name, if the database does not exist, SQLite does not actually create the database until it creates some content in the database.
2. Create a data table
SQLite> Create Table member (ID integer primary key, Name text, age integer, ADDR text );
Note: ID is the primary key, and This column has the property of automatic growth by default.
3. insert data
SQLite> insert into Member values (0, 'wwl', 21, 'shanghai'); // The column id = 0 must not exist; otherwise, an error occurs.
Or SQLite> insert into member (name, age, ADDR) values ('wwl', 21, 'shanghai ');
3. query data
SQLite>. Mode Column
SQLite>. headers on
SQLite> select * from member;
Note: The two commands before the SELECT statement (. headers and. mode) are used to improve the display format.
4. Create views and Indexes
SQLite> Create view schema as select * from member;
SQLite> Create index member_idx on member (ID)
5. Export data
You can use the. Dump command to export database objects to the SQL format. Without any parameters,. Dump exports the entire database as a database Definition Language (DDL) or database operation language (DML) command. It is suitable for recreating database objects and data in the database. If a parameter is provided, shell parses the parameter as the table name or view and exports any tables or views that match the given parameter. Otherwise, those that do not match will be ignored.
By default, the output of the. Dump command is directed to the screen. For example,. Dump
To redirect the output to a file, run the. Dump [filename] command to redirect all the output to the specified file. To restore the output to the screen, run the. Output stdout command.
SQLite>. output file. SQL
SQLite>. Dump
SQLite>. Output stdout
NOTE: If file. SQL does not exist, the file will be created in the current working directory. If the file exists, it will be overwritten.
6. Import Data
There are two ways to import data, depending on the file format to be imported. If the file is composed of SQL statements, you can use the. read command to import the commands contained in the file. If the file contains values separated by commas (,) or other delimiters (comma-swparated values, CSV), you can use. import [Table] command, which parses the specified file and tries to insert data into the specified table.
The. read command is used to import files created by the. Dump command. If you use the file. SQL exported as a backup file, you must first remove the existing database object and then re-import it using the following method:
SQLite> drop table member;
SQLite> drop view schema;
SQLite>. Read File. SQL
7. Back up the database
There are two ways to back up the database, depending on the type of backup you want. SQL dumping is the best portable backup.
The standard way to generate a dump is to use the CLP. Dump command: sqlite3 test. DB. Dump> test. SQL
In shell, you can redirect the output to an external file and execute the command to restore it to the screen output, for example:
SQLite>. output file. SQL
SQLite>. Dump
SQLite>. Output stdout
SQLite>. Exit
Similarly, it is easy to use SQL dump as the CLP input stream to import databases:
Sqlite3 test. DB <test. SQL
The knowledge of backing up a binary database is a little bit more work than copying files. You need to clear the database before the backup, so that you can release some deleted objects that are no longer in use. This database file will become smaller, so the binary copy will be smaller:
Sqlite3 test. DB vacuum
CP test. DB test. Backup
8. Other commands
SQLite> select last_insert_rowid (); // obtain the auto-increment value of the last insert.
SQLite>. Tabes // return all tables and views
SQLite>. Indices member // view the index of a table
SQLite>. schema member // get a table or view definition (DDL) statement. If the table name is not provided, return the Definition Statement of All database objects (table, view, index, triger ).