当前位置:文档之家› sqlite3数据库使用实例

sqlite3数据库使用实例

SQLite version 3.7.9 2011-11-01 00:52:41Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> create table students(...> id integer primary key,...> name text not null unique,...> sex int default 1,...> bak text);sqlite> .tablesstudentssqlite> .help.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 formatIf TABLE specified, only dump tables matchingLIKE 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 indicesIf TABLE specified, only show indices for tablesmatching 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 valuescolumn Left-aligned columns. (See .width)html HTML <table> codeinsert SQL insert statements for TABLEline One value per linelist Values delimited by .separator stringtabs Tab-separated valuestcl TCL list elements.nullvalue STRING Print STRING in place of NULL values.output FILENAME Send output to FILENAME.output stdout Send output to the screen.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 statementsIf TABLE specified, only show tables matchingLIKE 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 tablesIf TABLE specified, only list tables matchingLIKE pattern TABLE..timeout MS Try opening locked tables for MS milliseconds.width NUM1 NUM2 ... Set column widths for "column" mode.timer ON|OFF Turn the CPU timer measurement on or off sqlite> .schema studentsCREATE TABLE students(id integer primary key,name text not null unique,sex int default 1,bak text);sqlite> insert into students values('aa',0,'abc');Error: table students has 4 columns but 3 values were suppliedsqlite> insert into students(name) values('aaa');sqlite> select *from students...> ;1|aaa|1|sqlite> .showecho: offexplain: offheaders: offmode: listnullvalue: ""output: stdoutseparator: "|"stats: offinsertwidth:sqlite> .headers onsqlite> .separator "\t"sqlite> select *from students;id name sex bak1 aaa 1sqlite> insert into students(name,sex,bak) values('aaa',0,'test');Error: column name is not uniquesqlite> insert into students(name,sex,bak) values('bbb',0,'test');sqlite> select *from students;id name sex bak1 aaa 12 bbb 0 testsqlite> select *from students order by id desc...> ;id name sex bak2 bbb 0 test1 aaa 1sqlite> select *from student where bak is null;Error: no such table: studentsqlite> select *from students where bak is null;id name sex bak1 aaa 1sqlite> select count(*) from studetns;Error: no such table: studetnssqlite> select count(*) from students;count(*)2sqlite> select count(*) as count from students;count2sqlite> create table scores(...> id integer primary key,...> type text,...> score float not null check (socre > -1 and score < 101), ...> stu_id integer references students(id));Error: no such column: socresqlite> create table scores(...> id integer primary key,...> type text,...> score float not null check (score > -1 and score < 101), ...> stu_id integer references students(id));sqlite> insert into scores(type,socre,stu_id) values('c',70,3); Error: table scores has no column named socresqlite> insert into scores(type,score,stu_id) values('c',70,3); sqlite> select *from scores;id type score stu_id1 c 70.0 3sqlite> delete from scores;sqlite> select *from scores;sqlite> pragma foreign_keys on;Error: near "on": syntax errorsqlite> pragma foreign_keys=on;sqlite> insert into scores(type,score,stu_id) values('c',70,3);Error: foreign key constraint failedsqlite> insert into scores(type,score,stu_id) values('c',70,2); sqlite>。

相关主题