2011-06-20 81 views
0

我傳遞一個對象給我正在執行查詢的方法。我的方法是:通過SQLIte數據庫訪問一個對象

-(BOOL)searchWordInDatabase:(NSString *)string 
{ 
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory= [paths objectAtIndex:0]; 
NSString *path=[documentsDirectory stringByAppendingPathComponent:@"SymbolTalkLanguageElement.sqlite"]; 

//Open the database 
//might have to make database as property 
if(sqlite3_open([path UTF8String], &dataBase) ==SQLITE_OK) 
{ 
    const char *sql="select ImageName from tblLanguageElement where Category= string"; 
    sqlite3_stmt *statement; 

    if(sqlite3_prepare(dataBase, sql, -1, &statement, NULL) == SQLITE_OK) 
    { 
     while (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      NSLog(@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]); 
      //[list addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]]; 
      return YES; 
     } 

    } 



} 
return NO; 

}

但我沒有收到查詢字符串的值。我該怎麼做?

回答

2

查詢中存在錯誤。你不能像這樣替換一個字符串。以下面的方式做。

NSString *sqlStr = [NSString stringWithFormat:@"select ImageName from tblLanguageElement where Category = '%@'", string]; 
char *sql = (char *)[sqlStr UTF8String]; 
1

確保您的數據庫存在於您檢索的路徑中。在執行語句時,您可以按如下方式綁定字符串變量。

const char *sql = "select ImageName from tblLanguageElement where Category = ?"; 
sqlite3_stmt *statement; 
if(sqlite3_prepare(dataBase, sql, -1, &statement, NULL) == SQLITE_OK) 
{ 
     sqlite3_bind_text(statement, 1, [string UTF8String], -1, SQLITE_TRANSIENT); 

     while (sqlite3_step(statement) == SQLITE_ROW) 
     { 
      NSLog(@"%@",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]); 
      //[list addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]]; 
     return YES; 
     } 
}