簡體   English   中英

SQLite3:數據庫創建為空嗎?

[英]SQLite3: the database was created empty?

我試圖在我的C程序上使用sqlite3創建一個帶有表的數據庫,但是該數據庫始終創建為空,盡管它是使用sqlite shell創建的非空數據庫,這是我的以下代碼:

int main(void)
{
    printf("hello\n");
    sqlite3 *sqdb;
    sqlite3_initialize();
    const char* db = "test";
    sqlite3_open(db, &sqdb);
    const char* stmt = "CREATE TABLE IF NOT EXISTS testtable(creationdate DATE, data VARCHAR);";
    sqlite3_stmt *ppstmt;
    if (sqlite3_prepare_v2(sqdb, stmt, -1, &ppstmt, 0)!=SQLITE_OK)printf("error!\n");
    sqlite3_finalize(ppstmt);
    getch();
    return 0;
}

請幫助我解決問題。

sqlite3_prepare_v2()僅會編譯SQL,但不會運行它。 在已編譯的語句上調用sqlite3_step()來運行它,或使用將prepare + step + finalize組合為一個函數調用的sqlite3_exec()

嘗試這個:

   int main(void)
{
    printf("hello\n");
    sqlite3 *sqdb;
    int ret;
    sqlite3_initialize();
    const char* db = "test.sqlite3";
    sqlite3_open(db, &sqdb);
    const char* stmt = "CREATE TABLE IF NOT EXISTS testtable(creationdate DATE, data VARCHAR);";
    sqlite3_stmt *ppstmt=NULL;

    ret=sqlite3_exec(sqdb,stmt,0,0,0);
    if(ret!=SQLITE_OK)printf("error!\n");
    else printf("Table added\n");
    sqlite3_finalize(ppstmt);
    sqlite3_close(sqdb);
    return 0;
}

請記住操作后要關閉數據庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM