SQLite Tutorial
|
|
|
|
| Articles Reviews Structured Query Language | |
| Written by Mike Chirico | |
| Monday, 27 November 2006 | |
|
Page 1 of 12
This article explores the power and simplicity of sqlite3, starting with common commands and triggers. It then covers the attach statement with the union operation, introduced in a way that allows multiple tables, in separate databases, to be combined as one virtual table, without the overhead of copying or moving data. Next, I demonstrate the simple sign function and the amazingly powerful trick of using this function in SQL select statements to solve complex queries with a single pass through the data, after making a brief mathematical case for how the sign function defines the absolute value and IF conditions. Although the sign function currently does not exist in sqlite3, it is very easy to create in the "/src/func.c" file so that this function will be permanently available to all sqlite applications. Normally, user functions are created in C, Perl, or C++, which is also documented in this article. sqlite3 has the ability to store "blob", binary data. The sample program in the download, "eatblob.c", reads a binary file of any size into memory and stores the data in a user-specified field. Common Commands $ sqlite3 test.db The database file test.db will be created, if it does not already exist. Running this command will leave you in the sqlite3 environment. There are three ways to safely exit this environment: .q, .quit, and .exit. $ sqlite3 test.db "create table t1 (t1key INTEGER After table t1 has been created, data can be inserted as follows: $ sqlite3 test.db "insert into t1 (data,num) values ('This is sample data',3);" As expected, doing a select returns the data in the table. Note that the primary key "t1key" auto increments; however, there are no default values for timeEnter. To populate the timeEnter field with the time, an update trigger is needed. Note that you should not use the abbreviation "INT" when working with the PRIMARY KEY. You must use "INTEGER" for the primary key to update. $ sqlite3 test.db "select * from t1 limit 2"; In the statement above, the limit clause is used, and only two rows are displayed. For a quick reference of SQL syntax statements available with SQLite, see the syntax page. There is an offset option for the limit clause. For instance, the third row is equal to the following: "limit 1 offset 2". $ sqlite3 test.db "select * from t1 order by t1key limit 1 offset 2"; The ".table" command shows the table names. For a more comprehensive list of tables, triggers, and indexes created in the database, query the master table "sqlite_master", as shown below. $ sqlite3 test.db ".table"
$ sqlite3 test.db ".dump" The contents of the ".dump" can be filtered and piped to another database. Below, table t1 is changed to t2 with the sed command, and it is piped into the test2.db database.
Triggers -- ******************************************************************** The AFTER specification in ..."insert_t1_timeEnter AFTER..." is necessary. Without the AFTER keyword, the rowid would not have been generated. This is a common source of errors with triggers, since AFTER is not the default, so it must be specified. If your trigger depends on newly-created data in any of the fields from the created row (which was the case in this example, since we need the rowid), the AFTER specification is needed. Otherwise, the trigger is a BEFORE trigger, and will fire before rowid or other pertinent data is entered into the field. $ sqlite3 test.db < trigger1 Now try entering a new record as before, and you should see the time in the field timeEnter. $ sqlite3 test.db "insert into t1 (data,num) values ('First entry with timeEnter',19);" The last value has timeEnter filled automatically with Coordinated Universal Time, or UTC. If you want localtime, use select datetime('now','localtime'). See the note at the end of this section regarding UTC and localtime. -- ******************************************************************* As you can see, the PRIMARY KEY and current UTC time have been updated correctly. |
|
| Last Updated ( Sunday, 06 January 2008 ) | |
| < Prev | Next > |
|---|







