sqlite3数据库使用实例

  sre

SQLite version 3.7.9 2011-11-01 00:52:41

Enter “.help” for instructions

Enter 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> .tables

students

sqlite> .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 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      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 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

.width NUM1 NUM2 …   Set column widths for “column” mode

.timer ON|OFF          Turn the CPU timer measurement on or off

sqlite> .schema students

CREATE 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 supplied

sqlite> insert into students(name) values(‘aaa’);

sqlite> select *from students

…> ;

1|aaa|1|

sqlite> .show

echo: off

explain: off

headers: off

mode: list

nullvalue: “”

output: stdout

separator: “|”

stats: offinsert

width:

sqlite> .headers on

sqlite> .separator “\t”

sqlite> select *from students;

id      name    sex     bak

1       aaa     1

sqlite> insert into students(name,sex,bak) values(‘aaa’,0,’test’);

Error: column name is not unique

sqlite> insert into students(name,sex,bak) values(‘bbb’,0,’test’);

sqlite> select *from students;

id      name    sex     bak

1       aaa     1

2       bbb     0       test

sqlite> select *from students order by id desc

…> ;

id      name    sex     bak

2       bbb     0       test

1       aaa     1

sqlite> select *from student where bak is null;

Error: no such table: student

sqlite> select *from students where bak is null;

id      name    sex     bak

1       aaa     1

sqlite> select count(*) from studetns;

Error: no such table: studetns

sqlite> select count(*) from students;

count(*)

2

sqlite> select count(*) as count from students;

count

2

sqlite> 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: socre

sqlite> 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 socre

sqlite> insert into scores(type,score,stu_id) values(‘c’,70,3);

sqlite> select *from scores;

id      type    score   stu_id

1       c       70.0    3

sqlite> delete from scores;

sqlite> select *from scores;

sqlite> pragma foreign_keys on;

Error: near “on”: syntax error

sqlite> pragma foreign_keys=on;

sqlite> insert into scores(type,score,stu_id) values(‘c’,70,3);

Error: foreign key constraint failed

sqlite> insert into scores(type,score,stu_id) values(‘c’,70,2);

sqlite>

LEAVE A COMMENT

Captcha Code