2012-12-18 15 views
1

我創建了一個使用SQLite管理器(firefox插件)的4列和5行的表。 現在我想從下面的代碼幫助從該表中獲取整個隨機行,但它不起作用,因爲我從來沒有使用過它..我可能做錯了什麼?在iOS中從SQL表中獲取特定行?

-(NSDictionary *)fetchQuestionsWithRowID:(NSInteger)rowID withDbPath:(NSString *)dbPath 
     { 

     NSDictionary *questDic=[NSDictionary dictionary]; 
      sqlite3 *database=nil; 
      if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) 
      { 
       const char *sql = "SELECT * FROM quizQuestions Where rowid=?"; 

       sqlite3_stmt *selectstmt; 

       int result = sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL); 

       if(result != SQLITE_OK) { 
        NSLog(@"Prepare-error #%i: %s", result, sqlite3_errmsg(database)); 
       } 

       if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) 
       { 
// confused here 
        sqlite3_bind_text16(selectstmt, 1, &rowID, -1, SQLITE_TRANSIENT); 
        while(sqlite3_step(selectstmt) == SQLITE_ROW) { 

         NSString *str = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)]; 

         NSLog(@"string is %@",str); 

        } 
       } 
      } 
      sqlite3_close(database); 
      return questDic; 
     } 

回答

0

與Hemant,看看下面的代碼可能是它幫助,但你需要根據你的需要來作出這樣的代碼,意味着需要更正表,等:

NSString* sqliteQuery = NULL; 
    sqliteQuery = [NSString stringWithFormat:@"SELECT roomImage,bookID,roomName,floorID,roomDesc,id FROM roomTbl"]; 
    sqlite3_stmt* statement; 
    if(sqlite3_prepare_v2(db, [sqliteQuery UTF8String], -1, &statement, NULL) == SQLITE_OK) 
    { 
     while(sqlite3_step(statement) == SQLITE_ROW) 
     { 
      NSMutableDictionary* dict = [NSMutableDictionary new]; 
      NSData* data = nil; 
      int length = sqlite3_column_bytes(statement, 0); 
      data  = [NSData dataWithBytes:sqlite3_column_blob(statement, 0) length:length]; 
      [dict setObject:[Base64 encode:data] forKey:@"imageFloorBytes"]; 
      int idFR = sqlite3_column_int(statement, 1); 
      [dict setObject:[NSString stringWithFormat:@"%d",idFR] forKey:@"BookingID"]; 

      [dict setObject:[NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(statement, 2)] forKey:@"roomName"]; 
      [dict setObject:[NSString stringWithFormat:@"%d",sqlite3_column_int(statement, 3)] forKey:@"floorID"]; 
      [dict setObject:[NSString stringWithFormat:@"%s",(const char*)sqlite3_column_text(statement, 4)] forKey:@"roomDesc"]; 
      [dict setObject:[NSString stringWithFormat:@"%d",sqlite3_column_int(statement, 5)] forKey:@"roomID"]; 
      [arr addObject:dict]; 
      [dict release]; 
     } 
    } 
    // Finalize and close database. 
    sqlite3_finalize(statement); 

而且,你導入D b?而且,在做任何操作之前還有一件事你必須去檢查下面的文件或數據庫是否存在。

希望它能整理出您的問題