2015-04-01 28 views
0

我的問題是,我不能從選擇查詢中獲取值,因爲它執行101錯誤。SQLite查詢語句 - 客觀C

我認爲我的問題是,它必須是在SELECT語句

的sqlite3 *數據庫中的報價;

NSString * path = [[[ NSBundle mainBundle ]resourcePath ] stringByAppendingPathComponent : @"cars.sqlite"]; 
int returnCode = sqlite3_open ([ path cStringUsingEncoding : NSUTF8StringEncoding], &database) ; 

sqlite3_stmt * statement ; 
char * st ; 
NSString *querySQL = [NSString stringWithFormat: @"select brand,model from cars where brand=%@;", tmpbrand]; 

const char *query_stmt = [querySQL UTF8String]; 
st = sqlite3_mprintf (query_stmt); 

if(sqlite3_prepare_v2 (database , query_stmt , -1 ,& statement ,NULL)==SQLITE_OK){ 
returnCode = sqlite3_step (statement) ; // this statement returns a record 

while (returnCode == SQLITE_ROW) // while there are rows 
{ 
    models*tmp=[[models alloc]init]; 

    char* n=(char*)sqlite3_column_text(statement,0); 
    tmp=[NSString stringWithCString:n encoding:NSUTF8StringEncoding]; 
    char* model=(char*)sqlite3_column_text(statement, 1); 
    tmp.model=[NSString stringWithCString:model encoding:NSUTF8StringEncoding]; 


    [data addObject:tmp]; 

    returnCode = sqlite3_step (statement) ; // this statement returns a record 

} 
    sqlite3_finalize(statement); 
} 

}

回答

1

快速瀏覽一下the documentation顯示錯誤101的意思是 「完成」。

SQLITE_DONE結果代碼指示操作已完成。

如果你沒有得到你期望的行,你是對的,這可能是你的查詢。爲了使它工作,你可能需要在'%@'附近放置單引號。

但是,這不是正確的做法。你應該使用準備好的語句。也就是說,您的查詢看起來像select brand,model from cars where brand=?,並使用綁定函數填充值。

+0

我試圖做到這一點與單引號,我couldnt設法使它的工作..如果有可能向我展示如何使用預處理語句來做到這一點..我相當一個begginer在這.. – 2015-04-02 11:45:23

+0

這是第一個鏈接在Google搜索「objective c sqlite prepared statement」:http://stackoverflow.com/q/1187154/2998 – 2015-04-02 12:04:08