SQLite Basics


SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process. SQLite reads and writes directly to ordinary disk files. A complete SQL database with multiple tables, indices, triggers, and views, is contained in a single disk file.

Basic SQLite commands:

1, Create table

  1. CREATE TABLE testtable (id integer primary key, name text);

2, Insert into table

  1. INSERT INTO "testtable" VALUES(1,'Joseph');

3, Create a view

  1. CREATE VIEW testview AS select * from testtable;

4, Add index

  1. CREATE INDEX testindex on testtable(name);

5, Select command

  1. SELECT * from testtable;

6, Removing tables, triggers, views, and indexes

  1. drop index indexname
  2. drop table tablename
  3. drop trigger triggername
  4. drop view viewname

Export a database

You can export (backup) your database using the command-line program and .dump. It works the same way it does from the SQLite shell. But we are going to want the output to go to a file and not the screen so we will redirect it to a file called dbbackup.

  1. user@host:~$ sqlite3 test.db '.dump' > dbbackup

You could also compress the output:

  1. user@host:~$ sqlite3 test.db '.dump' | gzip -c > dbbackup.gz

The following example will use a file called statements.sql and import them into our test.db file:

  1. user@host:~$ sqlite3 test.db < statements.sql

Data types in SQLite v3

  • NULL - The value is a NULL value.
  • INTEGER - The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
  • REAL - The value is a floating point value, stored as an 8-byte IEEE floating point number.
  • TEXT - The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
  • BLOB - The value is a blob of data, stored exactly as it was input.

Comments

Priceless

Another priceless post from you guys. Thanks very much for the export instructions.
Ascentive

SQL

Thanks for the valuable information, I'm also getting the same error in exporting my db, any ideas?

Thank you

Boolean values

Please make a note that sqlite doesn't not support Boolean values.
Don't make the same mistakes I did...
:)

I can't export my db. There

I can't export my db. There is an error when I execute the command.

SQL

Again you have provided us with simple, yet concise, instructions for exporting and importing sql dbs. Thank you.

casinò online

Gamble at this casinò today and you will never want to gamble at any other casino again.