sqlite3_exec 的使用 [英] Use of sqlite3_exec

查看:55
本文介绍了sqlite3_exec 的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个 SQLITE3 命令可以生成一个包含超过 6000 万条记录的文件:

I have the next SQLITE3 commands that generates a file with more than 60 million records:

.mode csv
.output matppp.csv
select mat, ppp from matppp order by mat;
.output stdout

如何使用以下方法将这些命令包含到 C 程序中:

How can I include these commands into a C program using:

 sqlite3_exec(db, "..........", NULL, 0, &db_err); 

?

当我尝试自己做时,c 程序在执行时会产生一个表达式错误.

When I attempt to do it myself, the c program generates an expression error when executing.

谢谢!!

推荐答案

如果您想在 C 中执行此操作(而不是将某些内容通过管道传输到具有这些漂亮点命令的 sqlite3 命令行程序),那么您将必须使用回调.

If you want to do this within C (as opposed to piping something to sqlite3's command line program that has those nifty dot commands) then you will have to use a callback.

为了方便您剪切和粘贴,这里是从 Apophenia 库中提取的用于统计计算的代码.

For your cutting and pasting convenience, here is the code, hacked out of the Apophenia library for statistical computing.

第一部分:

sqlite3 *db=NULL; //The global database handle.

static int apop_db_open(char *filename){
    if (!filename)  
        sqlite3_open(":memory:",&db);
    else            
        sqlite3_open(filename,&db);
    if (!db)
        printf("Not sure why, but the database didn't open.\n");
    return 0;
}

//From the SQLite manual:
#define ERRCHECK {if (err!=NULL) {printf("%s\n",err); sqlite3_free(err);  return 0;}}

apop_data * apop_sqlite_query_to_screen(char *query){
  char *err = NULL;
    if (db==NULL) 
        apop_db_open(NULL);
    sqlite3_exec(db, query, The_Callback, a_param, &err); 
    ERRCHECK
}

第二部分:

回调将具有以下形式,并为返回的每一行运行一次.注意参数 a_param 如何传输;如果您不需要它(如本例所示),只需在上面将其设置为 NULL.

The callback will have the following form, and runs once for each line returned. Notice how the parameter a_param transfers; if you don't need it (as in this example), just set it to NULL above.

int The_Callback(void *a_param, int argc, char **argv, char **column){
    for (int i=0; i< argc; i++)
        printf("%s,\t", argv[i]);
    printf("\n");
    return 0;
}

这篇关于sqlite3_exec 的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆