int sqlite3_open(
constchar*filename,/*数据库文件名 */
sqlite3 **ppDb /* 创建的数据库连接对象*/
);
int sqlite3_close(sqlite3 */*打开的数据库连接对象*/);
constchar*sqlite3_errmsg(sqlite3*);
sqlITE_OK:表示打开成功
示例:
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
/*初始化连接对象开辟空间*/
int rc = sqlite3_open("sqlite.db",&db);
if(rc != sqlITE_OK)
{
fprintf(stderr,"%s\n",sqlite3_errmsg(db));
return-1;
}
printf("connect sucess!\n");
return0;
}
编译:gcc sqlite.c -o sqlite -I/usr/local/sqlite-3.3.7/include -L/usr/local/sqlite-3.3.7/lib -lsqlite3
int sqlite3_exec(
int(*callback)(void*,int,char**,char**),/* 回调函数*/
void*,/* 回调函数的第一个参数*/
char**errmsg /* 错误的消息*/
);
示例:
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
sqlite3 *db =NULL;
int rc = sqlite3_open("sqlite.db",&db);
if(rc != sqlITE_OK)
{
fprintf(stderr, sqlite3_errmsg(db));
return-1;
}
char*sql1 ="create table if not exists stu(id integer primary key default 1,name varchar(20));";
char*sql2 ="insert into stu(name) values('aa');";
char*errmsg;
rc = sqlite3_exec(db, sql1,NULL,&errmsg);
if(rc != sqlITE_OK)
{
fprintf(stderr,"错误%s\n",sqlite3_errmsg(db));
fprintf(stderr,errmsg);
}
rc = sqlite3_exec(db, sql2,errmsg);
}
sqlite3_close(db);
return0;
}
int sqlite3_get_table(
char***pazResult,/* 查询的结果集 */
int*pnRow, /* 结果集的行数*/
int*pnColumn, /*结果集的列数*/
);
释放结果集函数
void sqlite3_free_table(char**result);
示例:
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,开辟空间*/
rc = sqlite3_open("test.db",&db);
if(rc != sqlITE_OK )
{
fprintf(stderr,sqlite3_errmsg(db));
return-1;
}
char*sql ="select * from tbl_emp";
char**result =NULL;
int rows;
int cols;
rc = sqlite3_get_table(db,sql,&result,&rows,&cols,NULL);
if(rc != sqlITE_OK )
{
fprintf(stderr,sqlite3_errmsg(db));
return-1;
};
int i, flag=0;
for(i =0;i <(rows +1)* cols ;i++)
{
if((flag++)== cols)
{
flag=1;
printf("\n");
}
printf("%12s", result[i]);
}
printf("\n");
sqlite3_free_table(result);
sqlite3_close(db);
return1;
}
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,sqlite3_errmsg(db));
return-1;
}
char* name="denny";
char*sql;
char buff[200];
sprintf(buff,"select * from tbl_emp where name ='%s'",name);
sql =(char*)malloc(strlen(buff));
strcpy(sql,buff);
char**result =NULL;
int rows;
int cols;
rc = sqlite3_get_table(db,sqlite3_errmsg(db));
return-1;
};
int i, result[i]);
}
sqlite3_free_table(result);
printf("数据库连接成功!\n");
sqlite3_close(db);
return1;
}
回调函数
int(*callback)(
void*,/*从sqlite3_exec传递来的参数*/
int,/*结果集的列数*/
char**,/*列的值*/
char**/*列的名字*/
)
示例:
#include <stdio.h>
#include <sqlite3.h>
int testcallback(void*d,int cols,char**col_values,char**col_names)
{
int i;
int flag =*((int*)d);
if(flag ==1)
{
for(i =0;i< cols ;i++)
{
printf("%12s",col_names[i]);
}
printf("\n");
}
for(i =0;i< cols ;i++)
{
printf("%12s",col_values[i]);
}
printf("\n");
return0;
}
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,&db);
if(rc != sqlITE_OK )
{
fprintf(stderr,sqlite3_errmsg(db));
return-1;
}
printf("数据库连接成功!\n");
/*插入数据*/
char* sql ="select * from tbl_emp";
char* errmsg;
int flag =1;
rc = sqlite3_exec(db,testcallback,&flag,&errmsg);
if(rc != sqlITE_OK )
{
fprintf(stderr,errmsg);
return-1;
}
sqlite3_close(db);
return1;
}
预处理对象
int sqlite3_prepare(
int nByte,/* sql语句的长度 -1*/
sqlite3_stmt **ppStmt,/* sqlite3_stmt对象 */
constchar**pzTail /* 指向执行的sql语句 0*/
);
int sqlite3_bind_text(sqlite3_stmt*,constchar*,int n,void(*)(void*));
int sqlite3_step(sqlite3_stmt*);
constunsignedchar*sqlite3_column_text(sqlite3_stmt*,int iCol);
int sqlite3_finalize(sqlite3_stmt *pStmt);
示例:
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,sqlite3_errmsg(db));
return-1;
}
char* name ="denny";
char* sql ="select * from tbl_emp where name=? and id=?";
sqlite3_stmt *stmt =NULL;
rc = sqlite3_prepare(db,-1,&stmt,NULL);
sqlite3_bind_text(stmt,1,name,strlen(name),NULL);
sqlite3_bind_int(stmt,2,2);
sqlite3_step(stmt);
printf("id=%d\n",sqlite3_column_int(stmt,0));
printf("name=%s\n",sqlite3_column_text(stmt,1));
sqlite3_finalize(stmt);
printf("数据库连接成功!\n");
sqlite3_close(db);
return1;
}
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,sqlite3_errmsg(db));
return-1;
}
char* sql ="select * from tbl_emp";
sqlite3_stmt *stmt =NULL;
rc = sqlite3_prepare(db,NULL);
sqlite3_step(stmt);
printf("id=%d\n",1));
sqlite3_step(stmt);
printf("id=%d\n",1));
sqlite3_finalize(stmt);
printf("数据库连接成功!\n");
sqlite3_close(db);
return1;
}
预处理对象重复使用
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,sqlite3_errmsg(db));
return-1;
}
char* name ="annie";
char* sql ="select * from tbl_emp where name=?";
sqlite3_stmt *stmt =NULL;
rc = sqlite3_prepare(db,1));
printf("-------\n");
sqlite3_reset(stmt);
sqlite3_bind_text(stmt,"denny",strlen("denny"),释放空间*/
sqlite3_close(db);
return1;
}
事务
事务是数据库最小处理单元
事务的四个特性:
1.原子性
2.隔离性
3.一直性
4.持久性
sqlite如何使用事务
1.begin trasaction
2.rollback--回滚(取消)
3.commit--提交
4.sqlite3_exec(db,"begin trasaction",NULL)
5.sqlite3_exec(db,"commit",NULL)
6.sqlite3_exec(db,"rollback",NULL)
#include <stdio.h>
#include <sqlite3.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,sqlite3_errmsg(db));
return-1;
}
printf("数据库连接成功!\n");
/*插入数据*/
char* sql1 ="insert into tbl_emp(id,name,age,birthday) values(5,'denny',32,date('1977-06-28'))";
char* sql2 ="insert into tbl_emp(id,date('1977-06-28'))";
char* errmsg;
rc = sqlite3_exec(db,"begin transaction",&errmsg);
rc = sqlite3_exec(db,sql1,sql2,errmsg);
sqlite3_exec(db,"rollback transaction",&errmsg);
return-1;
}
sqlite3_exec(db,"commit transaction",&errmsg);
sqlite3_close(db);
return1;
}
其它:
#include <stdio.h>
#include <sqlite3.h>
#include <fcntl.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,sqlite3_errmsg(db));
return-1;
}
printf("数据库连接成功!\n");
/*插入数据*/
char* sql ="insert into tbl_emp(id,photo) values(7,?)";
sqlite3_stmt * stmt =NULL;
rc = sqlite3_prepare(db,NULL);
/*读取一个文件*/
int fd = open("logo.gif",O_RDONLY);
int buff[1024*8];
int len = read(fd,buff,sizeof(buff));
sqlite3_bind_blob(stmt,len,NULL);
sqlite3_step(stmt);
if(rc != sqlITE_OK )
{
fprintf(stderr,sqlite3_errmsg(db));
return-1;
}
sqlite3_close(db);
return1;
}
#include <stdio.h>
#include <sqlite3.h>
#include <fcntl.h>
int main(void)
{
/*定义一个数据库连接对象指针*/
sqlite3 *db =NULL;
int rc ;
/*初始化连接对象,sqlite3_errmsg(db));
return-1;
}
printf("数据库连接成功!\n");
/*插入数据*/
char* sql ="select photo from tbl_emp where id =7";
sqlite3_stmt * stmt =NULL;
rc = sqlite3_prepare(db,NULL);
sqlite3_step(stmt);
constvoid*pimage = sqlite3_column_blob(stmt,0);
int size = sqlite3_column_bytes(stmt,0);
/*读取一个文件*/
int fd = open("logo1.gif",O_WRONLY|O_CREAT,0777);
write(fd,pimage,size);
if(rc != sqlITE_OK )
{
fprintf(stderr,释放空间*/
sqlite3_close(db);
return1;
}