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>