繁体   English   中英

使用“C”执行API将sqlite3表导出到文件中

[英]Exporting the sqlite3 table into file using “C” execute API

我在fedora 14中使用sqlite3版本3.6.23.1。我能够使用这样的命令提示符将表导出到文件中,

sqlite3 data.db

sqlite> .output sample.txt;

sqlite> select *from sample;

我想在应用程序级别处理此案例。

我正在使用sqlite3开源“C”API在应用程序级别执行此命令。

execute("delete from sample:);// working fine

execute(".output sample.txt"); //Not working

它的抛出错误称为"SQL error in sqlite3_exec: near ".": syntax error"

请建议我如何使用此API创建文件以导入数据。

API的功能定义。

int execute(const char* fmt, ...)
{

    char *err_messg;

    int ret = 0, result = 0;

    char sql_string[1024] = ""; //this honestly needs to be more elegant; will do for now

    va_list args;

    va_start(args, fmt);

    ret = vsprintf(sql_string, fmt, args);

    va_end(args);

    printf("sql_string: %s\n", sql_string);

    if (!ret)
        result = 0;
    else
        result = 1;

    if (result != -1)
    {

        if (sqlite3_exec(db_conn, sql_string, NULL, 0, &err_messg) == SQLITE_OK)    //Actual API which will work with database.
        {
            return SUCCESS;
        }
        else
        {
            printf("\n SQL error in sqlite3_exec: %s\n", err_messg);
            return DBEXCE_FAIL;
        }
    }
    return SUCCESS;
}

您使用的SQLite API调用只接受SQL:
“sqlite3_exec()接口在作为第一个参数传入的数据库连接的上下文中运行零个或多个UTF-8编码的,以分号分隔的SQL语句传递到其第二个参数。” [来自http://www.sqlite.org/c3ref/exec.html]

如果浏览SQLite源,可以看到.output命令打开一个文件,然后使用sqlite3_snprintf API将查询结果的内容写入打开的文件句柄。

显示正在发生的事情的最新来源是:
http://www.sqlite.org/src/artifact/076e1c90d594644f36027c8ecff9a392cf2d3a06

.output的相关部分是这样的:

if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){
    if( p->outfile[0]=='|' ){
      pclose(p->out);
    }else{
      output_file_close(p->out);
    }
    p->outfile[0] = 0;
    if( azArg[1][0]=='|' ){
      p->out = popen(&azArg[1][1], "w");
      if( p->out==0 ){
        fprintf(stderr,"Error: cannot open pipe \"%s\"\n", &azArg[1][1]);
        p->out = stdout;
        rc = 1;
      }else{
        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
      }
    }else{
      p->out = output_file_open(azArg[1]);
      if( p->out==0 ){
        if( strcmp(azArg[1],"off")!=0 ){
          fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]);
        }
        p->out = stdout;
        rc = 1;
      } else {
        sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]);
      }
    }

所述.output SQLite的壳的命令是外壳的特征(与所有开头的其他命令一起. )。 如果您正在使用C接口,则应该执行查询以获取所需的行,并迭代它们,将它们一次写入文件。

根据本教程中的示例代码进行调整......

static int callback(void *handle, int argc, char **argv, char **azColName) {
     FILE *f = handle;
     int i;
     const char *sep = "";
     for (i=0;i<argc;i++) {
         fprintf(f, "%s\"%s\"", sep, argv[i]);
         sep = ", ";
     }
     fprintf(f, "\n");
     return 0;
}
const char *sql = "SELECT * FROM sample;";
sqlite3 *db;
FILE *f = fopen("sample.csv", "w"); // ought to check for errors here; demo code!
char *errs = NULL;

if (sqlite3_open("data.db", &db)) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    exit(1);
}
if (sqlite3_exec(db, sql, callback, f, &errs) != SQLITE_OK)
    fprintf(stderr, "SQL error: %s\n", errs);
sqlite3_close(db);

该帖子似乎有点旧,但我只想更新任何寻找类似信息的访问者。 有一个用于SQLite3数据库导入/导出功能的开源C / C ++ API。

可以通过此处访问API

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM