Tuesday, November 23, 2010

Keynotes to Master SQLLite via Command Line Shell

Type sqlite3 followed by the name the file that holds the SQLite database. If the file does not exist, a new one is created automatically.
Make sure you type a semicolon at the end of each SQL command! Control-D to terminated it
The database schema in an SQLite database is stored in a special table named "sqlite_master", but you can NOT drop table, update, insert or delete again the master table.
There is sqlite_temp_master table
. to execute the prepackaged query, like
.backup ?DB? FILE(backup to file);
.dump ?TABLE? ... (Dump database to SQL test format);
.import FILE TABLE (Import data from FILE to TABLE);
.indeces TABLE(Show all index on TABLE)
.iotrace FILE          Enable I/O diagnostic logging to FILE
.mode MODE ?TABLE?     Set output mode where MODE is one of:csv,html, insert,etc. (Default is |)
.output FILENAME       Send output to FILENAME
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.separator ", "        Defalt is '|',
Here is one of example: (Do you understand what/why I am doing this?).mode list
.separator |
.output test_file_1.txt
select * from tbl1;
SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1;
.table
.exit
Others Useful Information;
sqlite> .explain
sqlite> explain delete from tbl1 where two<20;
Using sqlite3 in a shell script with example
$ sqlite3 ex1 'select * from tbl1' | awk '{printf "<tr><td>%s<td>%s\n",$1,$2 }'
Compile source code from SQLite website via
gcc -o sqlite3 shell.c sqlite3.c -ldl -lpthread
Finally, I have a Question(Homework)?How we can dump the whole database into file in ASC,CSV or insert statement, so we can easy to migrate to other database?

No comments:

Post a Comment