iOS Sqlite3 SQLITE_ERROR [英] iOS Sqlite3 SQLITE_ERROR
问题描述
我在 iOS 应用程序中有一个函数,可以从数据库中读取一些数据.功能极其简单:
I have a function in an iOS app which reads in some data from a database. The function is extremely simple:
-(void) readCategories {
sqlite3 *database;
if([[NSFileManager defaultManager] fileExistsAtPath:databasePath]){
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "SELECT * FROM Categories ORDER BY name COLLATE NOCASE ASC";
sqlite3_stmt *compiledStatement;
int errorCode = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if(errorCode == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
[categories addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
}
} else {
NSLog(@"Error reading categories. Code: %d, message: '%s'", errorCode,sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
} else {
NSLog(@"Error opening DB");
}
} else {
NSLog(@"DB does not exist.");
}
}
问题是错误代码总是 SQLITE_ERROR,根据文档是:SQL 错误或缺少数据库".给出的消息是:'没有这样的表:类别'
The problem is that the errorCode is always SQLITE_ERROR which according to the documentation is: "SQL error or missing database". The message given is: 'no such table: Categories'
现在,如果我查看计算机上的数据库文件,该表显然就在那里.我也可以对它运行完全相同的查询并且它可以正常工作.
Now, if I look at the database file on my computer, the table is clearly there. I can also run exactly the same query on it and it works correctly.
有人知道出了什么问题吗?
Does anyone have any ideas on what is going wrong?
谢谢.
推荐答案
所以问题在于它没有复制数据库文件.我仍然不知道为什么不是.幸运的是,我能够以编程方式创建数据库文件.这样做解决了问题.不知道为什么文件不会复制.
So the bug was that it wasn't copying over the database file. I still have no idea why it wasn't. I fortunately have the luxury of being able to create the database file programatically. Doing that solved the problem. Not sure why the file wouldn't copy however.
这篇关于iOS Sqlite3 SQLITE_ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!