SQLite 不编译查询 - 目标 C



我对SQLite函数sqlite3_prepare_v2有问题,它总是返回1个错误代码。我使用的是用于IOS4的SQLite包装器SQLitemmanager。以前在没有使用包装器的情况下也发生过同样的错误,我切换到它,因为尽管该语句是用UTF8编码的,但错误仍然发生。我用调试器检查了数据库路径,它是正确的,所以我迷路了。。。顺便说一下,数据库是正确打开和关闭的。

这是代码的pice:

- (NSArray *)getRowsForQuery:(NSString *)sql {
    NSMutableArray *resultsArray = [[NSMutableArray alloc] initWithCapacity:1];
    if (db == nil) {
        [self openDatabase];
    }
    sqlite3_stmt *statement;    
    const char *query = [sql UTF8String];
    int prepareStatus = sqlite3_prepare_v2(db, query, -1, &statement, NULL);

    while (sqlite3_step(statement) == SQLITE_ROW) {

非常感谢你的帮助。

这里是传递给包装器对象的参数:

- (void)viewDidLoad
{
    [super viewDidLoad];
    dbManager = [[SQLiteManager alloc] initWithDatabaseNamed:@"XLO.sqlite"];
    SArray *provinciaArray = [dbManager getRowsForQuery:[NSString stringWithFormat:@"SELECT provincia FROM provincias;"]];

谢谢!

彼得,在这里:

- (NSError *) openDatabase {
    NSError *error = nil;
    NSString *databasePath = [self getDatabasePath];
    const char *dbpath = [databasePath UTF8String];
    #ifdef DEBUG
        NSLog(@"SQL result: <%s>", dbpath );
    #endif
    int result = sqlite3_open(dbpath, &db);
    if (result != SQLITE_OK) {
            const char *errorMsg = sqlite3_errmsg(db);
            NSString *errorStr = [NSString stringWithFormat:@"The database could not be opened: %@",[NSString stringWithCString:errorMsg encoding:NSUTF8StringEncoding]];
            error = [self createDBErrorWithDescription:errorStr andCode:kDBFailAtOpen];
    }
    return error;
}

非常感谢大家!

因为有人说数据库表不存在,所以您可能没有按照您认为的路径打开数据库。请记住,sqlite是被动的——它将在第一次写入时创建一个数据库。

在模拟器中运行应用程序并打印出路径。然后转到终端中的那个路径,使用sqlite cmdline确认数据库在那里,并且它有表。

最新更新