gpt4 book ai didi

ios - 如果 (sqlite3_step(compiledStatement) == SQLITE_ROW) 失败

转载 作者:行者123 更新时间:2023-11-29 12:51:28 31 4
gpt4 key购买 nike

在第一个实现文件上,所有步骤

- (void) readCitiesFromDatabase {

databaseName = @"123.sqlite";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

// Setup the database object
sqlite3 *database;

// Setup the SQL Statement and compile it for faster access
NSString *querySQL = [NSString stringWithFormat:@"SELECT City_Name, City_Picture, City_Id FROM CITIES, COUNTRIES WHERE CITIES.Country_Id=COUNTRIES.Country_Id AND COUNTRIES.Country_Id = '%@'", self.countryID];

const char *sqlStatement = [querySQL UTF8String];
sqlite3_stmt *compiledStatement;

// Init the countries Array
self.cities = [[NSMutableArray alloc] init];

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSLog(@"success");

if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(@"success");
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSLog(@"success");
// Read the data from the result row
char *temp_cName = (char *)sqlite3_column_text(compiledStatement, 0);
NSString *cName = temp_cName == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cName];

char *temp_cPicture = (char *)sqlite3_column_text(compiledStatement, 1);
NSString *cPicture = temp_cPicture == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cPicture];

char *temp_cId = (char *)sqlite3_column_text(compiledStatement, 2);
NSString *cId = temp_cId == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_cPicture];

NSMutableDictionary *city = [[NSMutableDictionary alloc] init];
[city setObject:cName forKey:@"City_Name"];
[city setObject:cPicture forKey:@"City_Picture"];
[city setObject:cId forKey:@"City_Id"];

[self.cities addObject:city];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}

现在我得到了城市列表。当我选择一个城市时,我看不到城市信息。这是第二次实现。

- (void) readCityInfoFromDatabase:(NSString *)querySQL
{
databaseName = @"123.sqlite";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

// Setup the database object
sqlite3 *database;

// Setup the SQL Statement and compile it for faster access

const char *sqlStatement = [querySQL UTF8String];
sqlite3_stmt *compiledStatement;

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSLog(@"success");
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(@"success");
if (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSLog(@"success");

char *temp_content = (char *)sqlite3_column_text(compiledStatement, 0);
[self.cityTextView setText:temp_content == NULL ? nil : [[NSString alloc] initWithUTF8String:temp_content]];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}

前两步是成功的,但是,

    if (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
NSLog(@"success");

我看不到这一步成功。

请帮忙。

最佳答案

你真的应该保存 sqlite3_step() 的结果并检查它是否:

  • SQLITE_ROW = 检索到的数据行
  • SQLITE_DONE = 没有(更多)数据要检索
  • 其他 = 发生了一些错误

不过,根据您分享的内容,您很可能只是收到了 SQLITE_DONE,因为没有找到任何东西。如果 SQL 中有错误(例如错误的连接、不正确的 WHERE 子句等),您可能会得到它。如果没有看到 readCityInfoFromDatabase 使用的 SQL 和数据库中的一些代表性数据,很难说。

顺便说一句,不仅对于 sqlite3_step(),而且对于所有的 sqlite3_xxx() 调用,如果您收到错误,您应该记录 sqlite3_errmsg()。现在,如果出现错误,您必须猜测问题的根源是什么。

关于ios - 如果 (sqlite3_step(compiledStatement) == SQLITE_ROW) 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22336332/

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