您的位置:首页 > 运维架构 > Linux

linux C语言 数据库sqlite3的添加,删除,查看

2016-11-01 23:17 183 查看
inux下数据库sqlite3的添加,删除,查看。

这个程序需要先在命令下创建表。

程序:

<span style="font-family:SimSun;font-size:18px;">
#include <sqlite3.h>

#include <stdio.h>
#include <stdlib.h>

static sqlite3 *db = NULL;
static char *errmsg = NULL;
char ** result = NULL;
int num = 0;
int display(void *para,int ncolumn, char ** columnvalue,char *columnname[])//回调函数,用于显示
{
int i;
num++;
printf("num = %d\n",num);
printf("total column is %d\n",ncolumn);
for(i = 0; i < ncolumn; i++)
{
printf("%s %s\n",columnname[i],columnvalue[i]);
}
printf("===============================\n");
return 0;
}

int main()
{
int re;
int row;
int column;
int i;
int j;

re = sqlite3_open("test.db",&db);
if(re != 0)
{
printf("open error\n");
exit(1);
}

//sqlite3_exec(db,"insert into test values('chengzi8','321');",0,0,&errmsg);//添加
//sqlite3_exec(db,"delete from test where phone = '321';",0,0,&errmsg);//删除
sqlite3_exec(db,"select * from test;",display,NULL,&errmsg);//显示2使用回调函数
printf("%d\n",num);
#if 0
sqlite3_get_table(db,"select * from test;",&result,&row,&column,&errmsg);//显示

for(i = 0; i <= row; i++)
{
for(j = 0; j < column; j++)
{
printf("%s|",result[i * column + j]);
}
printf("\n");
}
sqlite3_free_table(result);//注销结果集
#endif
re = sqlite3_close(db);//关闭
if(re != 0)
{
printf("open error\n");
exit(1);
}
}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐