1

我想从表中选择数据,但那些在问题表和答案表中具有相同问题 ID 的数据。现在它从答案中选择所有数据,代码如下所示

     + (void) getAnswers:(NSString*)dbPath{

     CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

    const char *sql = "select *from answers";

     sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

        while(sqlite3_step(selectstmt) == SQLITE_ROW) {

            NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
            Answers *coffeeObj = [[Answers alloc] initWithPrimaryKey:primaryKey];


            coffeeObj.answer_text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];



            NSString*testString=coffeeObj.answer_text;

            NSLog(testString);
            [appDelegate.answerArray addObject:coffeeObj];

            int mycount=[appDelegate.answerArray count];

            NSLog(@"This is int of latest %d",mycount);


            [coffeeObj release];
           }
           }
           }


     else
         sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
         }
4

1 回答 1

0

我建议避免调用 SQLite DB 的 c 级代码 - 尝试对 SQLite 进行简单的包装 - 请参阅https://github.com/JohnGoodstadt/EasySQLite

这将允许:

DataTable* result = [_db  ExecuteQuery:@"SELECT * FROM answers"];

感觉就像您需要使用 LEFT JOIN 语句来回答您的实际查询 - 如果您添加有关您的表定义的信息以及您想要获取的数据,我可以提供更多帮助。

于 2012-09-14T16:43:43.830 回答