【问题标题】:fetch a particular row from SQL table in iOS?从 iOS 中的 SQL 表中获取特定行?
【发布时间】:2012-12-18 20:28:12
【问题描述】:

我使用 SQLite Manager(firefox 插件)创建了一个包含 4 列和 5 行的表。 现在我想借助以下代码从该表中获取整个随机行,但它不起作用,因为我以前从未使用过它。我可能做错了什么??

-(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;
        }

【问题讨论】:

    标签: ios database sqlite


    【解决方案1】:

    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);
    

    另外,你导入你的数据库了吗?而且,在进行任何操作之前,您还必须检查以下文件或数据库是否存在。

    希望它能解决您的问题

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      相关资源
      最近更新 更多