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

linux下数据库sqlite3的添加,删除,查看。

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


程序:

#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);
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值