xcode sqlite3_prepare_v2 正在获取 SQLITE_ERROR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5136027/
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-14 20:20:18  来源:igfitidea点击:

sqlite3_prepare_v2 is getting SQLITE_ERROR

objective-cxcodesqlite

提问by Mark

I have been at this for hours and MUST get this working! It is holding up an iPhone app release... My first time using SQLite. I have followed all the advice and yet my sqlite3_prepare_v2 call gets a SQLITE_ERROR (1) every time!

我已经在这里工作了几个小时,必须让它工作!它阻碍了 iPhone 应用程序的发布......我第一次使用 SQLite。我遵循了所有建议,但我的 sqlite3_prepare_v2 调用每次都会收到 SQLITE_ERROR (1)!

Here is my code from my controller:

这是我的控制器中的代码:

        NSString *query = @"SELECT * FROM QandA ORDER BY random() LIMIT 1";
//  const char *sqlStatement = "SELECT * FROM QandA ORDER BY random() LIMIT 1";
    sqlite3_stmt *compiledStatement;

    // sqlite3_stmt *statement;
    int prepareStatus = sqlite3_prepare_v2(database, [query UTF8String],
                                           -1, &compiledStatement, NULL);
    if (prepareStatus == SQLITE_OK) {...

You'll note that I've tried using a "char *" also to no avail (among other attempts). My database opens fine with:

您会注意到我尝试过使用“char *”也无济于事(在其他尝试中)。我的数据库打开正常:

    databaseName = @"Facts.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
NSLog(@"databasePath = %@", databasePath);

int dbOpenStatus = sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_READWRITE, NULL);

From my controller interface:

从我的控制器界面:

    NSString *databaseName;
NSString *databasePath;

I've checked in the debugger and everything looks good, but the prepare statement fails. I don't know how to log the statement it is trying to compile... I assume/hope it is just what my SELECT says.

我已经检查了调试器,一切看起来都很好,但是准备语句失败了。我不知道如何记录它试图编译的语句......我假设/希望这正是我的 SELECT 所说的。

Can anyone help? I'm desperate. Mark

任何人都可以帮忙吗?我很绝望。标记

回答by Mark

Found the answer here. I had to use this instead for the path to the DB file:

这里找到了答案。我不得不使用它来代替 DB 文件的路径:

[[NSBundle mainBundle]pathForResource:@"Facts"extension:@"sqlite"];

This gave a slightly different path (one extra directory) - once I used that it worked! Hope this helps someone else... I spent many hours on this.

这给出了一个稍微不同的路径(一个额外的目录) - 一旦我使用它就可以了!希望这对其他人有帮助......我花了很多时间在这上面。

回答by Chainfire

Please note that the above is slightly inaccurate, the ofType should be used not extension, and the ofType obviously needs to match your file extension.

请注意,上面的说法有点不准确,ofType 应该使用而不是扩展名,而ofType 显然需要匹配您的文件扩展名。

If your DB is named "mysqldatabase.sql" change your path URL to:

如果您的数据库名为“mysqldatabase.sql”,请将您的路径 URL 更改为:

databasePath = [[NSBundle mainBundle]pathForResource:@"mysqldatabase" ofType:@"sql"];

回答by Ern? Simonyi

The code seems to be fine, might be a silly question but have you created the QandA table inside of the database?

代码似乎很好,可能是一个愚蠢的问题,但是您是否在数据库中创建了 QandA 表?