1

我正在像这样从 iphone sqlite 获取数据。我在表中有 6 条记录。

我的表有字段responses_Id、participant_Id、answer_option、answer_text、update_date_time,只有responses_Id和participant_Id之外的都是varchar字段)

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

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

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

    const char *sql = "select * from survey_question_responses";
    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);
            Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
                            coffeeObj.participant_Id=(This is integerin table)
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];

            NSString*test=coffeeObj.answer_option;



            [appDelegate.coffeeArray addObject:coffeeObj];

            int count=[appDelegate.coffeeArray count];

            NSLog(test);
            NSLog(@"count is %d",count);


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

当我 NSlog 数据时,它不显示任何东西。

4

3 回答 3

3

查看SQLite 文档。查询的所有结果值都在此处定义。您可以使用sqlite3_column_int类似于sqlite3_column_text文本的整数

于 2012-09-05T06:58:30.173 回答
2
NSNumber *primaryKey = [NSNumber numberWithInt:(int)sqlite3_column_int(selectstmt, 0)];
于 2012-09-05T07:01:55.250 回答
0
while(sqlite3_step(selectAllStmt) == SQLITE_ROW)
        {
            NSLog(@"record found");
            char *temp = (char *)sqlite3_column_text(selectAllStmt, 0);
            if (temp!=nil) {
                intId=[[NSString stringWithUTF8String:temp] intValue];
            }
            temp =NULL;
            // [pool release];
        }

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

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

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

    const char *sql = "select * from survey_question_responses";
    sqlite3_stmt *selectstmt;
    if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

        while(sqlite3_step(selectstmt) == SQLITE_ROW) {
    NSInteger primaryKey;
NSInteger intparticipant_Id;


            char *temp = (char *)sqlite3_column_text(selectstmt, 0);
            if (temp!=nil) {
                primaryKey=[[NSString stringWithUTF8String:temp] intValue];
            }
            temp =NULL;
            Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];


temp = (char *)sqlite3_column_text(selectstmt, 1);
            if (temp!=nil) {
                intparticipant_Id=[[NSString stringWithUTF8String:temp] intValue];
            }
            temp =NULL;
                            coffeeObj.participant_Id=intparticipant_Id;

            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
            coffeeObj.answer_option = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 5)];

            NSString*test=coffeeObj.answer_option;



            [appDelegate.coffeeArray addObject:coffeeObj];

            int count=[appDelegate.coffeeArray count];

            NSLog(test);
            NSLog(@"count is %d",count);


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


Note:- char *temp = (char *)sqlite3_column_text(selectAllStmt, 0);
            if(temp != NULL)
                str = [NSString stringWithUTF8String:temp];
            else
                str = @"";
            temp =NULL;

If you want to take integer then you can do type casting "str = [NSString stringWithUTF8String:temp];" here......
于 2012-09-05T07:38:19.807 回答