我有这段代码可以保存数组中的记录:
if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK) {
NSString *stringTable = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE id = ?",table];
const char *sql = [stringTable UTF8String];
sqlite3_prepare_v2(myDatabase, sql, -1, &statement2, NULL);
sqlite3_bind_int(statement2, 1, value);
while (sqlite3_step(statement2) == SQLITE_ROW){
[arrayValues addObject:((char *)sqlite3_column_text(statement2, 1)) ? [NSString stringWithUTF8String: (char *)sqlite3_column_text(statement2, 1)] : @""];
[arrayValues addObject:((char *)sqlite3_column_text(statement2, 2)) ? [NSString stringWithUTF8String: (char *)sqlite3_column_text(statement2, 2)] : @""];
...
...
[arrayValues addObject:((char *)sqlite3_column_text(statement2, 21)) ? [NSString stringWithUTF8String: (char *)sqlite3_column_text(statement2, 21)] : @""];
}
}
我试图做的就是找到一种方法来自动化这段代码并让我花很多时间输入重复的命令,为此,我尝试创建一个int并随着while而增加:
int temp = 1;//If 0 he get ID and I don't like to get...
...
while (sqlite3_step(statement2) == SQLITE_ROW){
[arrayValues addObject:((char *)sqlite3_column_text(statement2, temp)) ? [NSString stringWithUTF8String: (char *)sqlite3_column_text(statement2, temp)] : @""];
temp++;
}
}
但是这个方法只返回一个对象(即第1行),为什么会发生这种情况,以及如何解决这个问题呢?
答案 0 :(得分:0)
将你的循环放在 while
循环中
while (sqlite3_step(statement2) == SQLITE_ROW){
for (int temp = 1: temp <= 21; temp++) {
[arrayValues addObject:((char *)sqlite3_column_text(statement2, temp)) ? [NSString stringWithUTF8String: (char *)sqlite3_column_text(statement2, temp)] : @""];
}
}