7

I am using sqlite3 version 3.6.23.1 in fedora 14.I am able to export table into the file using command prompt like this,

sqlite3 data.db

sqlite> .output sample.txt;

sqlite> select *from sample;

I want handle this case at the application level.

I am using sqlite3 open source "C" API to execute this command in application level.

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

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

its throwing error called "SQL error in sqlite3_exec: near ".": syntax error"

Please suggest me how do i create a file to import the data using this API.

Function definition of the 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() インターフェースは、1 番目の引数として渡されたデータベース接続のコンテキストで、2 番目の引数に渡された 0 個以上の UTF-8 でエンコードされ、セミコロンで区切られた SQL ステートメントを実行します。 ." [http://www.sqlite.org/c3ref/exec.html より]

SQLite のソースを参照すると、.output コマンドがファイルを開き、sqlite3_snprintf API を使用して、開いたファイル ハンドルにクエリ結果の内容を書き込むことがわかります。

何が起こっているかを示す最新のソースは次のとおりです

.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

.outputSQLite シェルのコマンドは、 ( で始まる他のすべてのコマンドと共に)そのシェルの機能です.。C インターフェイスを使用している場合は、クエリを実行して必要な行を取得し、それらを繰り返し処理して一度に 1 つずつファイルに書き込む必要があります。

このチュートリアルにあるサンプル コードからの適応…</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 に答える