xcode ios - 打开sqlite数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11252173/
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
ios - open sqlite database
提问by Ashish Agarwal
I am trying to read data from a sqlite database and display it in a table view in an ios app. But I am not able to open the sqlite database for some reason and the app crashes with the message - Failed to open database.
我正在尝试从 sqlite 数据库读取数据并将其显示在 ios 应用程序的表视图中。但是由于某种原因我无法打开 sqlite 数据库并且应用程序崩溃并显示消息 - 无法打开数据库。
I created the database using the Firefox sqlite manager and copied the file into the Xcode project.
我使用 Firefox sqlite 管理器创建了数据库并将文件复制到 Xcode 项目中。
Here's my code -
这是我的代码 -
-(NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:kFilename];
}
I am trying to open the database using the sqlite3_open_v2 command inside the viewDidLoad
我正在尝试使用 viewDidLoad 中的 sqlite3_open_v2 命令打开数据库
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");
}
回答by Andreas
This is how I open my database. I have not been doing SQLite for a long time and I am not able to give you much help, other than providing this code that I use in my own application. This is from a class that I use in my app.
这就是我打开数据库的方式。我已经很长时间没有做 SQLite 了,除了提供我在自己的应用程序中使用的这段代码之外,我无法给你太多帮助。这是来自我在我的应用程序中使用的一个类。
static sqlite3 *database;
static sqlite3_stmt *enableForeignKey;
@implementation DBAdapter
+ (sqlite3 *)sharedInstance {
if (database == NULL) {
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"klb_db.sqlite"];
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
//NSLog(@"Database Successfully Opened :)");
database = newDBconnection;
if (sqlite3_prepare_v2(database, "PRAGMA foreign_keys = ON", -1, &enableForeignKey, NULL) != SQLITE_OK) {
NSLog(@"ERROR IN PRAGMA!");
}
sqlite3_finalize(enableForeignKey);
} else {
NSLog(@"Error in opening database :(");
database = NULL;
}
}
return database;
}
回答by Rob
Am I correct in assuming that your question was answered during our discussion in the comments of ios - sqlite prepare statement? Namely, we want to make sure that the database is included in the bundle (by confirming the "Copy Bundle Resources" setting in Xcode for your target's "Build Phases") and that you want want your app to check for the existence of the database in your documents folder and if it's not present, you'll copy it from your bundle ([[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:kFilename]
) to your documents folder.
我假设您的问题在我们的ios-sqlite prepare 声明的评论中的讨论中得到了回答是否正确?也就是说,我们要确保数据库包含在包中(通过确认 Xcode 中目标的“构建阶段”的“复制包资源”设置),并且您希望应用程序检查数据库的存在在您的文档文件夹中,如果它不存在,您将它从您的包 ( [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:kFilename]
)复制到您的文档文件夹。
This may be beyond the scope of your original question, but as you contemplate the eventual deployment of your app, and more importantly, subsequent upgrades/releases of your app, you might want to add some table to your database with some internal database version number that you can use to determine what version of the database the user has in their documents folder, and do any modifications to that database that you might need to support the latest versions of your app. Just a thought.
这可能超出了您原始问题的范围,但是当您考虑应用程序的最终部署,更重要的是,应用程序的后续升级/发布时,您可能希望将一些表添加到您的数据库中,其中包含一些内部数据库版本号您可以使用它来确定用户在其文档文件夹中拥有的数据库版本,并对该数据库进行任何修改以支持您的应用程序的最新版本。只是一个想法。
Anyway, if you still have any open questions, let us know. Otherwise I'll assume we've resolved your question.
无论如何,如果您还有任何悬而未决的问题,请告诉我们。否则我会假设我们已经解决了你的问题。