一、常用命令介绍
在终端下运行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 int primary key, name text, score real);
sqlite> create table stu (no int primary key, name text not null, score real);
备注:1)默认字段值可以为空;2)下面的命令,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) --复合主键
)
删除表
sqlite>drop table <table_name>
sqlite>drop table student
查询表中所有记录
sqlite>select * from <table_name>;
按指定条件查询表中记录
sqlite>select * from <table_name> where <expression>;
sqlite> select * from student
sqlite>select * from student where score is not null;
sqlite> select * from student where name=’zhao’
sqlite> select * from student where name=’zhao’ and score >=95
sqlite> select id, name from student where name=’zhao’ and score >=95
sqlite> select count(*) from student where score>90
sqlite>select * from student order by score desc;
sqlite>select * from student order by score asc;
向表中添加新记录
sqlite>insert into <table_name> values (value1, value2,…);
sqlite> insert into student values(1, ‘zhao’, 92);
sqlite> insert into student(no, name) values(2, ‘li’);
按指定条件删除表中记录
sqlite>delete from <table_name> where <expression>
sqlite> delete from student where score<60;
更新表中记录
sqlite>update <table_name> set <f1=value1>, <f2=value2>… where <expression>;
sqlite> update student set score=0;
sqlite> update student set name=’sun’ where no=3;
sqlite>update student set score=100, name='chenyong' where no=2;
在表中添加字段
sqlite>alter 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
二、常用编程接口介绍
1) int sqlite3_open(char *path, sqlite3 **db);
功能:打开sqlite数据库
path: 数据库文件路径
db: 指向sqlite句柄的指针
返回值:成功返回0,失败返回错误码(非零值)
2) int sqlite3_close(sqlite3 *db);
功能:关闭sqlite数据库
返回值:成功返回0,失败返回错误码
3) const char *sqlite3_errmg(sqlite3 *db);
返回值:返回错误信息
4) typedef int (*sqlite3_callback)(void *, int, char **, char **);
int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void *, char **errmsg);
功能:执行SQL操作
db:数据库句柄
sql:SQL语句
callback:回调函数
errmsg:错误信息指针的地址
返回值:成功返回0,失败返回错误码
不需要回调函数的情况:有关插入或更新的sql语句。
if (sqlite3_exec(db, “delete from table1 where id = 1”, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf(“error : %s\n”, errmsg);
exit(-1);
}
需要回调函数的情况:有关查询的sql语句。
int callback(void *para, int f_num, char **f_value, char **f_name)
{
int i;
printf(“*****************************\n”);
for (i=0; i<f_num; i++)
{
printf(“%s : %s\n”, f_name[i], f_value[i]);
}
return 0;
}
if (sqlite3_exec(db, “select * from table”, callback, NULL, &errmsg) != SQLITE_OK)
{
printf(“error : %s\n”, errmsg);
exit(-1);
}
代码输出如下:
no:2
name:zhao
score:86
no:3
name:wang
score:86
不使用回调函数执行SQL语句:
if (sqlite3_get_table(db, “select * from table”, &resultp, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
{
printf(“error : %s\n”, errmsg);
exit(-1);
}
index = ncolumn; // 第一条记录的第一个字段的下标
for (i=0; i<nrow; i++)
{
for (j=0; j<ncolumn; j++)
{
printf(“%s : %s\n”, resultp[j], resultp[index++]);
}
}
for (i=0; i<(nrow+1)* ncolumn; i++)
{
printf(“%s ”, resultp[i];
}
Printf(“\n”);
代码输出如下:
no:2
name:zhao
Score:86
no:3
name:wang
Score:86
no name score 2 zhao 86 3 wang 86
EXEC sp_rename 'customers.[contact title]', 'title', 'COLUMN'