Introduction to SQLite Database

Source: Internet
Author: User
Tags sqlite database win32

SQLite is an open source embedded database engine written by D.richard Hipp in C language. It supports most of the SQL92 standards and can be run on all major operating systems.

SQLite consists of the following sections: SQL compiler, Kernel, backend, and attachments. SQLite makes it easier to debug, modify, and extend SQLite's kernel by leveraging virtual machines and virtual database engines (VDBE). All SQL statements are compiled into an easy-to-read assembly that can be executed in an SQLite virtual machine. The overall structure of SQLite is shown below:

It is worth mentioning that the pocket-sized SQLite can support up to 2TB of database size, each database is in the form of a single file, which is stored in the form of B-TREE data structure on disk.

In terms of transactional processing, SQLite implements independent transactions with exclusive and shared locks at the database level. This means that multiple processes can read data from the same database at the same time, but only one can write data. An exclusive lock must be obtained before a process or thread wants the database to perform a write operation. After an exclusive lock is obtained, other read or write operations will no longer occur.

SQLite takes a Dynamic data type, and when a value is inserted into the database, SQLite checks its type, and if the type does not match the associated column, SQLite attempts to convert the value to the type of the column, and if it cannot, the value is stored as its own type, which SQLite calls " Weak type ". However, there is a special case, if the integer PRIMARY KEY, then the other type will not be converted, will be reported a "datatype Missmatch" error.

In summary, SQLite supports NULL, Integer, REAL, text, and BLOB data types, representing null values, integer values, floating-point values, string literals, and binary objects, respectively.

Now, let's do the SQLite database in person.

Before the operation, friends to download the SQLite database, the official download page is http://sqlite.org/download.html, I was under Windows test, so I chose precompiled Binaries for Windows Sqlite-shell-win32-x86 and sqlite-analyzer-win32-x86 zip packages, the former is the SQLite database engine, the latter is the SQLite Database Analyzer, mainly used to analyze the status of the database and other information , you can also according to their own situation to download. After the download is completed, respectively, to get two executable files,

These two files can be placed according to their preferences in the specified location, I put it in the D packing directory. Now let's take a step-by-stage operation of SQLite:

To create a database:

[SQL]View Plaincopy
  1. D:\>sqlite3 test.db
  2. SQLite version 3.7.7.1 2011-06-28 17:39:05
  3. Enter '. Help ' for instructions
  4. Enter SQL statements terminated with a ";"
  5. sqlite>. Databases
  6. Seq name File
  7. ---  ---------------  ----------------------------------------------------------
  8. 0 Main D:\test.db
  9. Sqlite>

We executed the sqlite3 command, the parameter is the name of the database, if the database already exists, then use, if not present, create a new, here we simply created the test.db in the current location, you can also create your own database in any existing and writable directory. (If you are not familiar with SQLite commands, you can perform the ". Help" command to list all the command listings for viewing).

To create a table:

[SQL]View Plaincopy
  1. sqlite> CREATE TABLE person (id INTEGER PRIMARY KEY autoincrement, name VARCHAR (a), age smallint);
  2. Sqlite>. Tables
  3. Person
  4. Sqlite>. Schema Person
  5. CREATE TABLE person (id INTEGER PRIMARY KEY autoincrement, name VARCHAR (a), age SMALLINT);
  6. Sqlite>

After we have created the table, we can use the ". Tables" command to see the existing tables, use the ". Schema" command to see the structure of the table, and if there are no table names for the parameters, the table statement will be output for all tables.

Insert data:

[SQL]View Plaincopy
    1. sqlite> INSERT into person VALUES (NULL, ' John ', 30);
    2. Sqlite> SELECT * from person ;
    3. 1|john|30

Import data from a. sql file:

[SQL]View Plaincopy
    1. Sqlite>. Read Test.sql
    2. Sqlite> SELECT * from person ;
    3. 1|john|30
    4. 2|david|35
    5. 3|henry|40
    6. Sqlite>

Analyze Database Usage Status:

[SQL]View Plaincopy
  1. D:\>sqlite3_analyzer test.db
  2. /** disk-Space Utilization Report for test.db
  3. Page size in bytes ......... ..... 1024x768
  4. Pages in the whole file (measured) .... 4
  5. Pages in the whole file (calculated): 4
  6. Pages that store data ....... ..... 4 100%
  7. Pages on the freelist (per header) .... 0 0.0%
  8. Pages on the freelist (calculated) .... 0 0.0%
  9. Pages of auto-vacuum overhead ..... 0 0.0%
  10. Number of tables in the database ... 4
  11. Number of indices ........ ....... 0
  12. Number of named indices ...... ..... 0
  13. Automatically generated indices ... 0
  14. Size of the file in bytes ....... 4096
  15. Bytes of user payload stored ..... 0.95%
  16. ...

To back up the database:

There are two ways to back up the SQLite database. If the database is in use, you should use the. dump command from the command-line interface. This creates a file that contains the necessary commands and data to recreate the database. The dump command can also be used to back up database tables.

[SQL]View Plaincopy
  1. sqlite>. Dump
  2. PRAGMA foreign_keys=OFF;
  3. BEGIN TRANSACTION;
  4. CREATE TABLE person (id INTEGER PRIMARY KEY autoincrement, name VARCHAR (a), age SMALLINT);
  5. INSERT into "person" VALUES (1,' John ', 30);
  6. INSERT into "person" VALUES (2,' David ', 35);
  7. INSERT into "person" VALUES (3,' Henry ', 40);
  8. DELETE from sqlite_sequence;
  9. INSERT into "Sqlite_sequence" VALUES (' person ', 3);
  10. COMMIT;
  11. Sqlite>. Output Dump.sql
  12. sqlite>. Dump
  13. Sqlite>

We can specify the output target as a file, and then use the command, the output information will be written to the specified file, if you want to revert to the standard output, you can set:

[SQL]View Plaincopy
  1. Sqlite>. Output stdout
  2. sqlite>. Dump
  3. PRAGMA foreign_keys=OFF;
  4. BEGIN TRANSACTION;
  5. CREATE TABLE person (id INTEGER PRIMARY KEY autoincrement, name VARCHAR (a), age SMALLINT);
  6. INSERT into "person" VALUES (1,' John ', 30);
  7. INSERT into "person" VALUES (2,' David ', 35);
  8. INSERT into "person" VALUES (3,' Henry ', 40);
  9. DELETE from sqlite_sequence;
  10. INSERT into "Sqlite_sequence" VALUES (' person ', 3);
  11. COMMIT;
  12. Sqlite>

If the database is not in use, you can copy the database files directly to a secure location.

Finally, we can use ". Quit" or ". Exit" to exit SQLite.

Management tools:

Now there are many SQLite management tools on the network, I recommend a useful tool for you: SQLite Expert.

Using SQLite in Java:

We want to use SQLite in Java, we need to download the SQLite related drivers, we recommend you go to the Http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC page to download the latest driver package, Now the latest version is Sqlite-jdbc-3.7.2.jar, which is a bit larger because it contains the native class library for Linux, Mac, and Windows.

After downloading the driver, we created a new project called SQLite:

In, we introduced the SQLite driver package to the CLASSPATH, and then set up a DB folder for the database file to be placed. Finally, let's take a look at the Test.java code:

[Java]View Plaincopy
  1. Package com.scott.sqlite;
  2. Import java.sql.Connection;
  3. Import Java.sql.DriverManager;
  4. Import Java.sql.ResultSet;
  5. Import java.sql.Statement;
  6. Public class Test {
  7. public static void Main (string[] args) throws Exception {
  8. Class.forName ("Org.sqlite.JDBC");
  9. Connection conn = drivermanager.getconnection ("jdbc:sqlite:db/test.db");
  10. Statement stmt = Conn.createstatement ();
  11. Stmt.executeupdate ("DROP TABLE IF EXISTS person");
  12. Stmt.executeupdate ("CREATE TABLE person (id INTEGER, name STRING)");
  13. Stmt.executeupdate ("INSERT into person VALUES (1, ' John ')");
  14. Stmt.executeupdate ("INSERT into Person VALUES (2, ' David ')");
  15. Stmt.executeupdate ("INSERT into Person VALUES (3, ' Henry ')");
  16. ResultSet rs = stmt.executequery ("select * from person");
  17. While (Rs.next ()) {
  18. System.out.println ("id=>" + rs.getint ("id") + ", name=>" + rs.getstring ("name"));
  19. }
  20. Stmt.close ();
  21. Conn.close ();
  22. }
  23. }

Execute the Test.java file with the following results:

This time, in our DB directory, a test.db file is generated:

Instructions for using SQLite:

There are currently no Web servers available for SQLite. The only way to run SQLite from an application that resides on another computer is to run from a network share. This can cause problems such as file locking issues with UNIX® and Windows® network shares. There are also performance degradation issues due to latency associated with accessing network shares.

SQLite only provides database-level locking.

SQLite does not have a user account concept, but instead determines the permissions of all databases based on the file system.

Conclusion:

Because of its low resource footprint, good performance, and 0 management costs, embedded databases have its niche, with built-in SQLite databases for developers, such as Android and iphone, whose ease of use accelerates application development and makes complex data storage much easier.

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.