ios - sqlite 准备语句 [英] ios - sqlite prepare statement

查看:31
本文介绍了ios - sqlite 准备语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 ios 应用程序中的 sqlite 数据库读取数据.当我运行应用程序时,它能够打开数据库,但在日志文件中它显示消息 - 准备语句有问题".我不知道我的准备语句有什么问题这是我的代码 -

I am trying to read data from a sqlite database in an ios app. When I run the app, it is able to open the database but in the log file it shows the message - "Problem with the prepare statement". I don't know what is wrong with my prepare statement Here's my code -

-(NSString *)dataFilePath{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

viewDidLoad 我有 -

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    myarray = [[NSMutableArray alloc]init];

    sqlite3 *database;
    if(sqlite3_open([[self dataFilePath]UTF8String], &database)!=SQLITE_OK){
        sqlite3_close(database);
       NSAssert(0, @"Failed to open database");
    }

    const char *createSQL = @"SELECT ID, TITLE FROM FIRST ORDER BY TITLE;"; //first is the table in the database
    sqlite3_stmt *sqlStmt;
    if(sqlite3_prepare_v2(database, [createSQL UTF8String], -1, &sqlStmt, nil)!=SQLITE_OK){
        NSLog(@"Problem with prepare statement"); //this is where the code gets stuck and I don't know why
    }else{

        while(sqlite3_step(sqlStmt)==SQLITE_ROW){
            NSInteger number = sqlite3_column_int(sqlStmt, 0);
            NSString *title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStmt, 1)];
            [myarray addObject:title];
        }
        sqlite3_finalize(sqlStmt);
    }
    sqlite3_close(database);
}

推荐答案

如果您的准备语句失败,而不仅仅是报告准备语句有问题",请尝试检索错误消息,例如,

If your prepare statement fails, rather than just reporting "Problem with prepare statement", try retrieving the error message, e.g.,

NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));

这可能会让您更好地指出问题所在.

This might give you a better indication of the problem.

我过去看到的一个问题是,可能找不到数据库(因为它没有包含在包中,名称中有错别字等),但是标准的 sqlite3_open如果它不存在,函数将创建它,因此 sqlite3_open 将成功,但在新创建的空白数据库中将找不到相关表.比 sqlite3_open 更好的是:

A problem I've seen in the past is that the database might not be found (because it wasn't included in the bundle, typo in the name, etc.) but the standard sqlite3_open function will create it if it's not there, and thus the sqlite3_open will succeed, but the table in question won't be found in the blank, newly created database. Better than sqlite3_open would be:

sqlite3 *database;
if (sqlite3_open_v2([[self dataFilePath] UTF8String], &database, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK) {
    sqlite3_close(database); // not sure you need to close if the open failed
    NSAssert(0, @"Failed to open database");
}

这样,如果找不到数据库,您就会收到警告.但是如果你已经完成了sqlite3_open,你可能有一个空白的数据库,所以你可能想重置你的模拟器和/或从你的设备中删除应用程序,然后再用sqlite3_open_v2.

That way you get a warning if the database is not found. But if you've done sqlite3_open already, you might have a blank database, so you might want to reset your simulator and/or remove the app from your device, before trying it with sqlite3_open_v2.

这篇关于ios - sqlite 准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆