Basics of SQLite Database

Source: Internet
Author: User

Basics of SQLite Database

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.

SQLite3 installation and basic operations

Simple Application of SQLite databases in Ubuntu 12.04

How to install SQLite in Ubuntu 12.04

2. Installation

SQLite on Windows

1) enter the SQL download page: http://www.sqlite.org/download.html

2) 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:

  1. /* For Debian or Ubuntu /*
  2. $ Sudo apt-get install sqlite3 sqlite3-dev
  3. /* For RedHat, CentOS, or Fedora /*
  4. $ 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.

  1. Sqlite3 test. db

Create a table:

  1. Sqlite> createtable mytable (id integerprimarykey, value text );
  2. 2 columns were created.

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:

  1. Sqlite> insertinto mytable (id, value) values (1, 'micheal ');
  2. Sqlite> insertinto mytable (id, value) values (2, 'jenny ');
  3. Sqlite> insertinto mytable (value) values ('francis ');
  4. Sqlite> insertinto mytable (value) values ('kerk ');

Query data:

  1. Sqlite> select * from test;
  2. 1 | Micheal
  3. 2 | Jenny
  4. 3 | Francis
  5. 4 | Kerk

Set to format the query result:

  1. Sqlite>. mode column;
  2. Sqlite>. header on;
  3. Sqlite> select * from test;
  4. Id value
  5. ------------------------
  6. 1 Micheal
  7. 2 Jenny
  8. 3 Francis
  9. 4 Kerk

. Mode column is set to column Display mode, And. header is used to display column names.

Modify the table structure and add columns:

  1. Sqlite> altertable mytable addcolumn email text notnull ''collate nocase ;;

Create View:

  1. Sqlite> createview nameview asselect * from mytable;

Create an index:

  1. Sqlite> createindex test_idx on mytable (value );

4. Some useful SQLite commands

Display table structure:

  1. Sqlite>. schema [table]

Obtain all tables and views:

  1. Sqlite>. tables

Obtain the index list of the specified table:

  1. Sqlite>. indices [table]

Export the database to an SQL file:

  1. Sqlite>. output [filename]
  2. Sqlite>. dump
  3. Sqlite>. output stdout

Import a database from an SQL file:

  1. Sqlite>. read [filename]

Format output data to CSV format:

  1. Sqlite>. output plugin filename.csv]
  2. Sqlite>. separator,
  3. Sqlite> select * from test;
  4. Sqlite>. output stdout

Import data from a CSV file to a table:

  1. Sqlite> createtable newtable (id integerprimarykey, value text );
  2. Sqlite>. import into filename.csv] newtable

Back up the database:

  1. /* Usage: sqlite3 [database]. dump> [filename] */
  2. Sqlite3 mytable. db. dump> backup. SQL

Restore database:

  1. /* Usage: sqlite3 [database] <[filename] */
  2. Sqlite3 mytable. db <backup. SQL

SQLite details: click here
SQLite: click here

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.