如何从iphone sqlite数据库获取int数据

时间:2012-09-05 06:51:54

标签: iphone xcode sqlite

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

我的表有字段respond_Id,participant_Id,answer_option,answer_text,update_date_time只有varchar字段,除了respond_Id和participant_Id)

    + (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数据时它没有显示任何东西。

3 个答案:

答案 0 :(得分:3)

查看SQLite Documentation。查询中的所有结果值都在那里定义。 您可以将sqlite3_column_int用于与文本

类似的sqlite3_column_text类似的整数

答案 1 :(得分:2)

NSNumber *primaryKey = [NSNumber numberWithInt:(int)sqlite3_column_int(selectstmt, 0)];

答案 2 :(得分: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......