******************************************************************
--------------部分操作--------------
建数据库:
[email protected]:~$ sqlite3 my.db
查看帮助:
sqlite> .help
文件存放位置:
sqlite> .database
退出:
sqlite> .quit
查看表:
sqlite> .tables
显示表的结构:
sqlite> .schema
*******************************************************************
---------------语法------------------
1.建表:
sqlite> create table usr(id integer primary key, name text,age integer null, gender text, salary real not null);
注意:没有默认字段,加单引号则为字符串,其他为整型,如果输入字符串不加单引号,如:123abc,则数据库会报错;
2.删除表
sqlite> drop table usr;
3.增:
sqlite> insert into usr(id, name, age, salary) values(2, 'liu', 20, 6000);
4.删
sqlite> delete from usr where id = 2;
5.改:
sqlite> update usr set gender = 'man' where id = 3;
6.查:
(1):查找表中所有内容
sqlite> select * from usr ;
(2):按指定条件查找表中内容
1)在表中查找id=2项的所有信息
sqlite> select * from usr where id = 2;
2)在表中超找id 大于3,小于10的项的所有信息
sqlite> select * from usr where id>=2 and id<=10;
3)在表中查找id等于3的选项,并显示其中的name 和age
sqlite> select name,age from usr id=3;
4)显示表中的前两项信息
sqlite> select * from usr limit 2;
5)以年龄排序显示表中信息
sqlite> select * from usr order by age ;
7.添加字段
alter table usr add column addr;
注意:添加字段只能为字符串格式
8.更新表中的记录
sqlite> update usr name='farsight' where id=88 and age=23;
9.更改表中的字段名
sqlite3> alter table usr rename column oldColumnName to newColumnName;
备注:
1)alter table usr rename to temp;
2)create table usr(id,name,height);
3)insert into usr select id,name,age from temp;
************************************************************
------------------编程接口--------------------------
网址:www.sqlite.org
1.打开数据库:
int sqlite3_open(char *path, sqlite3 **db);
db:指向sqlite句柄的指针;
成功返回0;
失败返回错误码;
2.出错判断
char *sqlite3_errmsg(sqlite3*);
///
3.执行sql操作
(1)int sqlite3_exec(
sqlite3*, /* 数据库句柄 */
const char *sql, /* SQL语句 */
int (*callback)(void*,int,char**,char**), /* 执行函数 */
void *, /* 函数参数 */
char **errmsg /* 错误信息指针的地址 */
);
///
执行函数:每找到一条自动执行一次函数
typedef int (*sqlite3_callback)(
void *para, //传递给函数的参数
int f_num, //记录中包含的字段的数目
char **f_value, //包含每个字段值的指针数组
char **f_name //包含每个字段名称的指针数组
);
//
sqlite3_exec 测试代码:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <string.h>
//查询到一条记录就会调用一次回调函数
/***********************************
*addr : 给回调函数传递的参数
*n_column : 记录的条数
*value : 保存查到的结果[指针数组]
*name : 保存字段名[指针数组]
**********************************/
int printf_info(void *addr,int n_column,char **value,char **name)
{
int i = 0;
//打印字段名
for(i = 0;i < n_column;i ++)
printf("%s\t",name[i]);
printf("\n");
//打印字段值
for(i = 0;i < n_column;i ++)
printf("%s\t",value[i]);
printf("\n");
return 0;
}
int exec_sql(sqlite3 *pdb,char *sql)
{
int ret;
char *errmsg;
ret = sqlite3_exec(pdb,sql,printf_info,NULL,&errmsg);
if(ret != SQLITE_OK){
printf("Error : %s.\n",errmsg);
return -1;
}
return 0;
}
//./a.out test.db
int main(int argc, const char *argv[])
{
int ret;
sqlite3 *pdb;
char buf[1024];
if(argc < 2)
{
fprintf(stderr,"Usage : %s argv[1].\n",argv[0]);
exit(EXIT_FAILURE);
}
//打开数据库文件
ret = sqlite3_open(argv[1],&pdb);
if(ret != SQLITE_OK){
printf("sqlite3_open %s : %s.\n",argv[1],sqlite3_errmsg(pdb));
exit(EXIT_FAILURE);
}
while(1)
{
printf("sqlite3>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
if(strncmp(buf,"quit",4) == 0)
break;
exec_sql(pdb,buf);
}
sqlite3_close(pdb);
return 0;
}
(2)int sqlite3_get_table(sqlite3 *db, const char *sql, char ***resultp, int*nrow, int *ncolumn, char **errmsg);
功能:执行SQL操作
db:数据库句柄
sql:SQL语句
resultp:用来指向sql执行结果的指针
nrow:满足条件的记录的数目
ncolumn:每条记录包含的字段数目
errmsg:错误信息指针的地址
返回值:成功返回0,失败返回错误码
sqlite3_get_table测试代码:
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int do_select(sqlite3 *pdb,char *sql)
{
int i = 0,j = 0;
char *errmsg;
char **result;
int n_row = 0;
int n_coloumn = 0;
int ret;
int Index;
ret = sqlite3_get_table(pdb,sql,&result,&n_row,&n_coloumn,&errmsg);
if(ret != SQLITE_OK){
fprintf(stderr,"Exec %s:%s.\n",sql,errmsg);
return -1;
}
Index = n_coloumn;
//查到记录
if(n_row != 0){
//输出字段名
for(i = 0;i < n_coloumn;i ++)
{
printf("%s\t",result[i]);
}
printf("\n");
//输出字段值
for(i = 0;i < n_row;i ++)
{
for(j = 0;j < n_coloumn;j ++)
{
printf("%s\t",result[Index]);
Index ++;
}
printf("\n");
}
}else{
printf("No result!.\n");
}
//释放存放记录的空间
sqlite3_free_table(result);
return 0;
}
//./a.out database
int main(int argc, const char *argv[])
{
char buf[1024];
int ret;
sqlite3 *pdb;
ret = sqlite3_open(argv[1],&pdb);
if(ret != SQLITE_OK){
fprintf(stderr,"sqlite3 open %s : %s.\n",argv[1],sqlite3_errmsg(pdb));
exit(EXIT_FAILURE);
}
while(1)
{
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
do_select(pdb,buf);
}
exit(EXIT_SUCCESS);
}
注意:考虑到sqlite3_exec 需要使用回调函数,在编程时是通常使用sqlite3_gettable(),用来执行打印操作,而使用sqlite3_exec()来执行其它的sql语句;