SQLite Quick Start Guide
1. Introduction
SQLite is an open-source embedded Relational Database Engine for SQL database with self-contained, zero configuration, and transaction support. It is highly portable, easy to use, compact, efficient, and reliable. Unlike other database management systems, SQLite is easy to install and run. In most cases, you can create, connect to, and use a database by ensuring that the binary file of SQLite exists. If you are looking for an embedded database project or solution, SQLite is definitely worth considering.
2. Installation
SQLite on Windows
Go to the SQL download page: http://www.sqlite.org/download.html
Download the pre-compiled Binary Package in Windows:
- Sqlite-shell-win32-x86-<build #>. zip
- Sqlite-dll-win32-x86-<build #>. zip
Note: <build #> is the sqlite compilation version.
Decompress the zip file to your disk and add the decompressed directory to the PATH variable of the system to execute the sqlite command in the command line.
Optional: If you plan to release an application based on the sqlite database, you also need to download the source code to compile and use its API
- Sqlite-amalgamation-<build #>. zip
SQLite on Linux
Multiple Linux releases provide convenient commands to obtain SQLite:
/* For Debian or Ubuntu /*$ sudo apt-get install sqlite3 sqlite3-dev /* For RedHat, CentOS, or Fedora/*$ yum install SQLite3 sqlite3-dev
SQLite on Mac OS X
If you are using Mac OS snow leopard or a newer version of the system, SQLite is installed on the system.
3. Create the first SQLite Database
Now that you have installed the SQLite database, create the first database. In the command line window, enter the following command to create a database named test. db.
sqlite3 test.db
Create a table:
sqlite> create table mytable(id integer primary key, value text);
This table contains a primary key field named id and a text field named value.
Note: at least one table or view must be created for the newly created database to save the database to the disk. Otherwise, the database will not be created.
Next, write some data to the table:
sqlite> insert into mytable(id, value) values(1, 'Micheal');sqlite> insert into mytable(id, value) values(2, 'Jenny');sqlite> insert into mytable(value) values('Francis');sqlite> insert into mytable(value) values('Kerk');
Query data:
sqlite> select * from mytable;1|Micheal2|Jenny3|Francis4|Kerk
Set to format the query result:
sqlite> .mode column;sqlite> .header on;sqlite> select * from mytable;id value----------- -------------1 Micheal2 Jenny3 Francis4 Kerk
. Mode column is set to column Display mode, And. header is used to display column names.
Modify the table structure and add columns:
sqlite> alter table mytable add column email text not null '' collate nocase;;
Create View:
sqlite> create view nameview as select * from mytable;
Create an index:
sqlite> create index test_idx on mytable(value);
4. Some useful SQLite commands
Display table structure:
sqlite> .schema [table]
Obtain all tables and views:
sqlite > .tables
Obtain the index list of the specified table:
sqlite > .indices [table ]
Export the database to an SQL file:
sqlite > .output [filename ] sqlite > .dump sqlite > .output stdout
Import a database from an SQL file:
sqlite > .read [filename ]
Format output data to CSV format:
sqlite >.output [filename.csv ] sqlite >.separator , sqlite > select * from test; sqlite >.output stdout
Import data from a CSV file to a table:
sqlite >create table newtable ( id integer primary key, value text ); sqlite >.import [filename.csv ] newtable
Back up the database:
/* usage: sqlite3 [database] .dump > [filename] */sqlite3 mytable.db .dump > backup.sql
Restore database:
/* usage: sqlite3 [database ] < [filename ] */ sqlite3 mytable.db < backup.sql