7

我在 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;
}
4

3 回答 3

5

您使用的 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]);
      }
    }
于 2012-07-02T08:34:49.363 回答
4

SQLite shell的.output命令是该 shell 的一个特性以及所有其他以 开头的命令.)。如果您正在使用 C 接口,则应该执行查询以获取所需的行,并遍历它们,一次将它们写入文件。

改编自本教程中的示例代码……</p>

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);
于 2012-07-02T09:09:54.813 回答
3

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

API 可以通过这里访问

于 2013-04-21T06:06:27.450 回答