SQLite常用命令及编程接口介绍.doc
文本预览下载声明
sqlite常用命令及编程接口介绍
一、常用命令介绍
在终端下运行sqlite3 *.db,出现如下提示符
SQLite version 3.7.2
Enter “.help” for instructions
Enter SQL statements terminated with a “;”
sqlite
*.db 是要打开的数据库文件。若该文件不存在,则自动创建。
显示所有命令
sqlite .help
退出sqlite3
sqlite.quit
显示当前打开的数据库文件
sqlite.database
显示数据库中所有表名
sqlite.tables
查看表的结构
sqlite.schema table_name
/*******************************************/
以下为SQL命令,每个命令以;结束
创建新表
create table table_name (f1 type1, f2 type2,…);
sqlite create table student(no integer primary key, name text, score real);
备注:下面的命令,sqlite3不支持
paper_name+author_id 构成复合主键
create table paper (
paper_name varchar(50) not null,
author_id char(10) not null,
constraint PK_paper primary key(paper_name,author_id) --复合主键
)
删除表
sqlitedrop table table_name
sqlitedrop table student
查询表中所有记录
sqliteselect * from table_name;
按指定条件查询表中记录
sqliteselect * from table_name where expression;
sqlite select * from student
sqlite select * from student where name=’zhao’
sqlite select * from student where name=’zhao’ and score =95
sqlite select count(*) from student where score90
向表中添加新记录
sqliteinsert into table_name values (value1, value2,…);
sqlite insert into student values(1, ‘zhao’, 92);
按指定条件删除表中记录
sqlitedelete from table_name where expression
sqlite delete from student where score60;
更新表中记录
sqliteupdate table_name set f1=value1, f2=value2… where expression;
sqlite update student set score=0;
sqlite update student set name=’sun’ where no=3;
在表中添加字段
sqlitealter table table add column field type;
sqlite alter table student add column gender integer default 0;
在表中删除字段
Sqlite中不允许删除字段,可以通过下面步骤达到同样的效果
sqlite create table stu as select no, name, score from student
sqlite drop table student
sqlite alter table stu rename to student
二、常用编程接口介绍
int sqlite3_open(char *path, sqlite3 **db);
功能:打开sqlite数据库
path: 数据库文件路径
db: 指向sqlite句柄的指针
返回值:成功返回0,失败返回错误码(非零值)
显示全部