xcode ios - sqlite 准备语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11242298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:43:18  来源:igfitidea点击:

ios - sqlite prepare statement

iosxcodesqlite

提问by Ashish Agarwal

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 -

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

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

In the viewDidLoadI have -

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);
}

回答by JiteshW

Several things you can try.

您可以尝试几件事。

  1. Clean your app, Remove from simulator or device & try installing a fresh copy again and see if it works.

  2. Open your DB in Terminal & try to run your sql statement at there. Check wether you are getting desired output.

  1. 清理您的应用程序,从模拟器或设备中删除并尝试再次安装新副本,看看它是否有效。

  2. 在终端中打开您的数据库并尝试在那里运行您的 sql 语句。检查您是否获得了所需的输出。

回答by Rob

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.

这可能会给您一个更好的问题指示。

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_openfunction will create it if it's not there, and thus the sqlite3_openwill succeed, but the table in question won't be found in the blank, newly created database. Better than sqlite3_openwould be:

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

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");
}

That way you get a warning if the database is not found. But if you've done sqlite3_openalready, 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.

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

回答by Liam George Betsworth

Try changing the nil to NULL. Also, try defining the SQL statement as a const char.

尝试将 nil 更改为 NULL。此外,尝试将 SQL 语句定义为 const char。

....

const char *sql = "SELECT ID, TITLE FROM FIRST ORDER BY TITLE";
sqlite3_stmt *sqlStmt;

if(sqlite3_prepare_v2(database, sql, -1, &sqlStmt, NULL)!=SQLITE_OK){

....