gpt4 book ai didi

ios - IO 中的 SQLite 只返回一行?

转载 作者:行者123 更新时间:2023-11-28 19:59:41 25 4
gpt4 key购买 nike

我正在尝试从我的 SQLite 中提取行,但出于某种原因,只提取了第一行。这是我的代码:

-(void)pullNewDataFromSqlite {

NSLog(@"pulling new data");

NSString *fetch_sql = [NSString stringWithFormat:@"SELECT * FROM %@", tableName];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *my_sqlfile = [[paths objectAtIndex:0] stringByAppendingPathComponent:databaseName];


if(sqlite3_open([my_sqlfile UTF8String], &my_dbname) == SQLITE_OK) {
NSLog(@"pulling new data, %@", fetch_sql);
sqlite3_stmt *statement;
NSLog(@"could not prepare statement: %s\n", sqlite3_errmsg(my_dbname));


if(sqlite3_prepare_v2(my_dbname, [fetch_sql UTF8String], -1, &statement, nil) == SQLITE_OK) {


int numberOfColumns = sqlite3_column_count(statement);
//sqlite3_


while (sqlite3_step(statement) == SQLITE_ROW) {
NSLog(@"NEW SQLITE ROW");

int sdcount = sqlite3_column_int(statement, 0);
NSLog(@"NEW SQLITE ROW number, %d", sdcount);


for(int a = 1; a < numberOfColumns; a++) {

char *field = (char *)sqlite3_column_text(statement, a);


NSString *fieldVal = [[NSString alloc]initWithUTF8String:field];
NSString *formatout = [NSString stringWithFormat:@"%@ ", fieldVal];


//match first column with first array

//adding each column info to the array in order

//user id
if(a == 1) {
NSLog(@"a is 1 and the value of the user id is %@", formatout);
[userIdsArray addObject:formatout];
}

//name
if(a == 2) {
NSLog(@"a is 2 and the value of the name id is %@", formatout);
[namesArray addObject:formatout];
}

//picture
if(a == 3) {
NSLog(@"a is 3 and the value of the pic id is %@", formatout);
[picturesArray addObject:formatout];
}




}
//"call sqlite3_finalize to clean up the memory used for the statement, then we return the data."
sqlite3_finalize(statement);

}

}
}

[self closeDB];

if(count < [userIdsArray count]) {
grabFollowers = YES;
}

count = [userIdsArray count];

[self.tableView reloadData];

}

它也确实显示了其他 2 行,但由于某种原因它显示为空白。

最佳答案

您正在 while 循环中调用 sqlite3_finalize

在遍历所有行之前不得调用它。

关于ios - IO 中的 SQLite 只返回一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com