一、常用命令介紹
在終端下運行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'