Xcode Sqlite选择下一个结果

时间:2014-10-15 15:45:02

标签: ios objective-c sqlite

我觉得我错过了明显的,下面的代码拉回了三个结果" 201501,201502& 201503"我想将每个结果放在自己的UITextfield中,我可以得到52个结果,任何人都可以帮助丢失代码,非常感谢

_startWeekText.text = 201501
_endWeekText.text = 201503


- (void)allResults{

    const char *dbpath_rm1 = [databasePath UTF8String];
    sqlite3_stmt *statementrm1;
    if (sqlite3_open(dbpath_rm1, &dataTV) == SQLITE_OK) {NSLog(@"Select db opened");
        NSString *querySQLPrm1 = [NSString stringWithFormat:

        @"SELECT Wk_Num  FROM trendViewData WHERE (Name =\"%@\" ) AND (Wk_Num >= \"%@\" AND Wk_Num <= \"%@\") ",_resultSearchText.text,_startWeekText.text,_endWeekText.text];


        const char *query_stmtrm = [querySQLPrm1 UTF8String];
        if (sqlite3_prepare_v2(dataTV,
                               query_stmtrm, -1, &statementrm1, NULL) == SQLITE_OK)

            while (sqlite3_step(statementrm1) == SQLITE_ROW) {
                NSString *rm1 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statementrm1, 0)];
                _wk1.text = rm1;

                NSString *rm2 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statementrm1, 0)];
                _wk2.text = rm2;

                NSString *rm3 = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statementrm1, 0)];
                _wk3.text = rm3;



            }

        sqlite3_finalize(statementrm1); }
    sqlite3_close(dataTV);NSLog(@"Select db Closed");


    NSLog(@"%@",_wk);
}

1 个答案:

答案 0 :(得分:0)

您可以拥有一系列UITextField引用,然后根据它填充:

NSArray *fields = @[_wk1, _wk2, _wk3];
NSInteger index = 0;

const char *dbpath_rm1 = [databasePath UTF8String];
sqlite3_stmt *statementrm1;
if (sqlite3_open(dbpath_rm1, &dataTV) == SQLITE_OK) {
    NSLog(@"Select db opened");

    const char *query_stmtrm = "SELECT Wk_Num  FROM trendViewData WHERE (Name = ?) AND (Wk_Num >= ? AND Wk_Num <= ?)";
    if (sqlite3_prepare_v2(dataTV, query_stmtrm, -1, &statementrm1, NULL) == SQLITE_OK) {

        if (sqlite3_bind_text(statementrm1, 1, [_resultSearchText.text UTF8String], -1, NULL) != SQLITE_OK) {
            NSLog(@"Bind 1 failed");
        }
        if (sqlite3_bind_text(statementrm1, 2, [_startWeekText.text UTF8String], -1, NULL) != SQLITE_OK) {
            NSLog(@"Bind 2 failed");
        }
        if (sqlite3_bind_text(statementrm1, 3, [_endWeekText.text UTF8String], -1, NULL) != SQLITE_OK) {
            NSLog(@"Bind 3 failed");
        }

        while (sqlite3_step(statementrm1) == SQLITE_ROW) {
            if (index < [fields count]) {
                UITextField *field = fields[index];
                NSString *rm = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statementrm1, 0)];
                field.text = rm;
                index++;
            } else {
                NSLog(@"Got more results than I was expecting!");
            }
        }

        sqlite3_finalize(statementrm1);
    } else {
        NSLog(@"prepare failed: %s", sqlite3_errmsg(dataTV));
    }

    sqlite3_close(dataTV);NSLog(@"Select db Closed");
} else {
    NSLog(@"open failed");
}

另外,请注意,如果任何SQLite调用失败并且我没有使用stringWithFormat来构建SQL,我会记录消息,而是使用sqlite_bind_text