2016-06-10 77 views
2

我剛從sqlite開始,需要一些快速幫助。我有以下代碼正在工作。在最後一個查詢中,例如"Select * from company where id = 2"。我只想檢查是否存在一排有id = 2。而已。但我無法做到這一點。回調方法對於所有查詢都很常見,因此無法對其進行修改,也不希望爲上次查詢編寫特定回調。如何檢查sqlite中是否存在記錄C

對於最後一個查詢,我可以只獲得0/1False/True嗎? 我試過的是,使用,計數行,但再次回調它打印結果,但不是0/1。

static int callback(void *NotUsed, int argc, char **argv, char **azColName){ 
int i; 
for(i=0; i<argc; i++){ 
     printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 
} 
printf("\n"); 
return 0; 
} 

int main() 
{ 
    sqlite3 *db; 
    char *zErrMsg = 0; 
    int rc; 
    char *sql; 

    /* Open database */ 
    rc = sqlite3_open("test.db", &db); 
    if(rc){ 
     fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); 
     exit(0); 
    }else{ 
     fprintf(stderr, "Opened database successfully\n"); 
    } 

    sql = "CREATE TABLE COMPANY(" \ 
     "ID INT PRIMARY KEY  NOT NULL," \ 
     "NAME   TEXT NOT NULL," \ 
     "AGE   INT  NOT NULL," \ 
     "ADDRESS  CHAR(50)," \ 
     "SALARY   REAL);"; 

    /* Execute SQL statement */ 
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); 
    if(rc != SQLITE_OK){ 
    fprintf(stderr, "SQL error: %s\n", zErrMsg); 
     sqlite3_free(zErrMsg); 
    }else{ 
     fprintf(stdout, "Table created successfully\n"); 
    } 

     /* Create SQL statement */ 
    sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \ 
     "VALUES (1, 'Paul', 32, 'California', 20000.00); " \ 
     "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \ 
     "VALUES (2, 'Allen', 25, 'Texas', 15000.00); "  \ 
     "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \ 
     "VALUES (3, 'Teddy', 23, 'Norway', 20000.00);" \ 
     "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \ 
     "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00);"; 

    /* Execute SQL statement */ 
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); 
    if(rc != SQLITE_OK){ 
     fprintf(stderr, "SQL error: %s\n", zErrMsg); 
     sqlite3_free(zErrMsg); 
    }else{ 
     fprintf(stdout, "Records created successfully\n"); 
    } 

    sql = "Select * from COMPANY where id = 2"; 

    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); 
    if(rc != SQLITE_OK){ 
     fprintf(stderr, "SQL error: %s\n", zErrMsg); 
     sqlite3_free(zErrMsg); 
    }else{ 
     fprintf(stdout, "Records created successfully\n"); 
    } 

    sqlite3_close(db); 
    return 0; 
} 
+0

爲什麼不能你寫這一個單獨的回調函數? –

+0

但我希望tor在rc中返回1或0,表示是否存在行。或者我如何獲得回調函數的返回值? – impossible

+0

回調函數的返回值指示是否發生錯誤,就像'sqlite3_exec'的返回值一樣。 –

回答

3

你可以Exec的查詢不同

sql = "Select * from COMPANY where id = 2"; 
    struct sqlite3_stmt *selectstmt; 
    int result = sqlite3_prepare_v2(db, sql, -1, &selectstmt, NULL); 
    if(result == SQLITE_OK) 
    { 
     if (sqlite3_step(selectstmt) == SQLITE_ROW) 
     { 
      // record found 
     } 
     else 
     { 
      // no record found 
     } 
    } 
    sqlite3_finalize(selectstmt); 
+0

我越來越核心愚蠢的__pthread_mutex_lock錯誤:斷言'互斥 - > __數據.__所有者== 0'失敗。我們可以使用其他類似SELECT EXISTS的東西嗎? – impossible

+0

不確定我得到你:看看[在這個手冊頁](http://www.techonthenet.com/sqlite/exists.php) – LPs

+1

我改變了你的答案,現在它爲我工作。請接受編輯。我接受了答案。感謝您的回答。 – impossible