前端之家收集整理的这篇文章主要介绍了
C语言操作sqlite数据库(增删改查),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>
#include <sqlite3.h>
int main()
{
const char *select_query="select * from users";
int ret = 0;
sqlite3 *db = 0;
char *s;
ret = sqlite3_open("./mydb",&db);
if(ret != sqlITE_OK)
{
printf("无法打开数据库\n");
return 1;
}
printf("数据库连接成功\n");
ret = sqlite3_exec(db,"create table if not exists users(id int(10),name char(20))",0,&s);
if(ret != sqlITE_OK)
{
sqlite3_close(db);
printf("create error\n");
return 1;
}
printf("create success\n");
ret = sqlite3_exec(db,"insert into users values(1,'aass')",&s);
ret += sqlite3_exec(db,"insert into users values(2,'bbbb')","insert into users values(3,'cccc')",&s);
if(ret != sqlITE_OK)
{
sqlite3_close(db);
printf("insert error\n");
return 1;
}
printf("insert success\n");
ret = sqlite3_exec(db,"delete from users where id=1",&s);
if(ret != sqlITE_OK)
{
sqlite3_close(db);
printf("delete error\n");
return 1;
}
printf("delete success\n");
ret = sqlite3_exec(db,"update users set name='qqq' where id=3",&s);
if(ret != sqlITE_OK)
{
sqlite3_close(db);
printf("update error\n");
return 1;
}
printf("update success\n");
int nrow,ncolumn;
char ** db_result;
ret = sqlite3_get_table(db,select_query,&db_result,&nrow,&ncolumn,&s);
if(ret != sqlITE_OK)
{
printf("select error\n");
sqlite3_close(db);
return 1;
}
int i,j;
for(i=0;i<(nrow+1)*ncolumn;i+=ncolumn)
{
for(j=0;j<ncolumn;j++)
{
printf("%s ",db_result[i+j]);
}
printf("\n");
}
sqlite3_close(db);
db = 0;
return 0;
}
原文链接:https://www.f2er.com/sqlite/199278.html