从数据库中选择数据形成iphone应用程序中的两个表

时间:2012-07-20 06:46:08

标签: iphone xcode sqlite

我想从表中选择数据,但是在问题表和答案表中选择具有相同问题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.
         }

1 个答案:

答案 0 :(得分:0)

我建议避免调用SQLite数据库的c级代码 - 尝试使用SQLite的简单包装 - 请参阅https://github.com/JohnGoodstadt/EasySQLite

这将允许:

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

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