SQLite
Par PlaceOweb le lundi, avril 1 2013, 15:49 - SQL - Lien permanent
Mémo SQLite, la base de données embarquée la plus utilisée (desktop, smartphones, ...)
Les outils pour SQLite
- Le shell SQLite en ligne de commandes : command-line shell
- Plugin Firefox : SQLite Manager
SQLite
- Documentation SQLite
- Datatypes : NULL, INTEGER, REAL, TEXT, BLOB
- Wikipedia : SQLite
SELECT * FROM sqlite_master; PRAGMA table_info (ma_table); PRAGMA index_list (ma_table); PRAGMA index_info (ma_table); PRAGMA database_list; PRAGMA collation_list;
sqlite> .s D:\>sqlite3 SQLite version 3.7.15.2 2013-01-09 11:53:05 Enter ".help" for instructions Enter SQL statements terminated with a ";"
sqlite> -- afficher l'aide sqlite> .h .backup ?DB? FILE Backup DB (default "main") to FILE .bail ON|OFF Stop after hitting an error. Default OFF .databases List names and files of attached databases .dump ?TABLE? ... Dump the database in an SQL text format If TABLE specified, only dump tables matching LIKE pattern TABLE. .echo ON|OFF Turn command echo on or off .exit Exit this program .explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off. With no args, it turns EXPLAIN on. .header(s) ON|OFF Turn display of headers on or off .help Show this message .import FILE TABLE Import data from FILE into TABLE .indices ?TABLE? Show names of all indices If TABLE specified, only show indices for tables matching LIKE pattern TABLE. .load FILE ?ENTRY? Load an extension library .log FILE|off Turn logging on or off. FILE can be stderr/stdout .mode MODE ?TABLE? Set output mode where MODE is one of: csv Comma-separated values column Left-aligned columns. (See .width) html HTML <table> code insert SQL insert statements for TABLE line One value per line list Values delimited by .separator string tabs Tab-separated values tcl TCL list elements .nullvalue STRING Use STRING in place of NULL values .output FILENAME Send output to FILENAME .output stdout Send output to the screen .print STRING... Print literal STRING .prompt MAIN CONTINUE Replace the standard prompts .quit Exit this program .read FILENAME Execute SQL in FILENAME .restore ?DB? FILE Restore content of DB (default "main") from FILE .schema ?TABLE? Show the CREATE statements If TABLE specified, only show tables matching LIKE pattern TABLE. .separator STRING Change separator used by output mode and .import .show Show the current values for various settings .stats ON|OFF Turn stats on or off .tables ?TABLE? List names of tables If TABLE specified, only list tables matching LIKE pattern TABLE. .timeout MS Try opening locked tables for MS milliseconds .trace FILE|off Output each SQL statement as it is run .vfsname ?AUX? Print the name of the VFS stack .width NUM1 NUM2 ... Set column widths for "column" mode .timer ON|OFF Turn the CPU timer measurement on or off
sqlite> -- lister les bases de données
PRAGMA database_list; 0|main| -- attacher/monter une base de donnée (fichier) ATTACH 'D:\maBase.sqlite' AS maBase; -- lister les bases de données PRAGMA database_list; 0|main| 2|maBase|D:\maBase.sqlite
sqlite> -- afficher les entêtes des colonnes sqlite> .header ON
sqlite> --PRAGMA (les informations sur bases, tables, index)
PRAGMA database_list; seq|name|file 0|main| 1|temp| 2|maBase|D:\maBase.sqlite
sqlite> -- CREATE TABLE
CREATE TABLE maTable(f_TEXT TEXT, f_NUMERIC NUMERIC, f_INTEGER INTEGER, f_REAL REAL, PRIMARY KEY(f_INTEGER ASC)); CREATE TABLE maTableNull(f1_not_null TEXT, f2_null TEXT NULL);
sqlite> -- schema : consulter les CREATE TABLE statements sqlite> .s CREATE TABLE maTable(f_TEXT TEXT, f_NUMERIC NUMERIC, f_INTEGER INTEGER, f_REAL REAL, PRIMARY KEY(f_INTEGER ASC)); CREATE TABLE maTableNull(f1_not_null TEXT, f2_null TEXT NULL);
sqlite> -- lister les tables sqlite> .tables
-- Lister les tables et leurs définitions SELECT name FROM sqlite_master WHERE type='table'; SELECT name FROM sqlite_temp_master WHERE type='table'; -- pour lister les tables temporaires -- Lister les éléments de la base (tables, définitions, ...) SELECT * FROM sqlite_master; type|name|tbl_name|rootpage|sql TABLE|maTable|maTable|2|CREATE TABLE maTable(f_TEXT TEXT, f_NUMERIC NUMERIC, f_INTEGER INTEGER, f_REAL REAL, PRIMARY KEY(f_INTEGER ASC)) TABLE|maTableNull|maTableNull|3|CREATE TABLE maTableNull(f1_not_null TEXT, f2_null TEXT NULL)
PRAGMA table_info(maTable); 0|f_TEXT|TEXT|0||0 1|f_NUMERIC|NUMERIC|0||0 2|f_INTEGER|INTEGER|0||1 3|f_REAL|REAL|0||0
BEGIN; INSERT INTO maTableNull VALUES ("NOT NULL", "NOT NULL"); INSERT INTO maTableNull VALUES ("NOT NULL", NULL); INSERT INTO maTableNull VALUES (NULL,NULL); COMMIT; SELECT * FROM maTableNull; f1_not_null|f2_null NOT NULL|NOT NULL NOT NULL| |sqlite> SELECT f2_null IS NULL FROM maTableNull; f2_null IS NULL 0 1 1
ATTACH 'D:\DATA\MaBase1.db' AS b1; ATTACH 'D:\DATA\MaBase2.db' AS b2; SELECT * FROM b1.sqlite_master; -- différences entre 2 tables SELECT * FROM b1.ma_table EXCEPT SELECT * FROM b2.ma_table; SELECT * FROM b2.ma_table EXCEPT SELECT * FROM b1.ma_table; -- les éléments identiques (intersection) entre 2 tables SELECT * FROM b2.ma_table INTERSECT SELECT * FROM b1.ma_table;