我注意到通过我的项目,我正在使用的SQLite的.db文件只有在我将它存储在我的iPhone模拟器的文档文件中时才有效。
如何将它存储在应用程序本身中,以便我可以在iPhone上使用db文件而不是模拟器?
也许这会有所帮助......
这就是我所说的:
- (void)createOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"localdb.db"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
const char *dbPath = [dbPathString UTF8String];
//creat db here
if (sqlite3_open(dbPath, &companyDB)==SQLITE_OK) {
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS storelist (StoreID NOT NULL PRIMARY KEY AUTOINCREMENT, StoreName TEXT NULL, StoreAddr TEXT NULL, StoreHost TEXT NULL, StorePort TEXT NULL, StoreUser TEXT NULL, StorePass TEXT NULL, StoreDB TEXT NULL)";
sqlite3_exec(companyDB, sql_stmt, NULL, NULL, NULL);
sqlite3_close(companyDB);
}
}
}
答案 0 :(得分:3)
你需要在你的appDelegate.m文件中有一个你调用的方法,如果它不存在则复制你的文件ex:
-(void) copyDatabaseFile
{
BOOL success;
NSFileManager *fileMAnager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSLog(@"document Directory:%@", documentDirectory);
NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"yourFile.db/"];
NSLog(@"Writable path:%@", writableDBPath);
success = [fileMAnager fileExistsAtPath:writableDBPath];
if (success) return;
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"yourFile.db/"];
NSLog(@"default database path:%@", defaultDBPath);
success = [fileMAnager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success)
NSAssert1(0,@"\failed to create writable database file with message'%@'.\"", [error localizedDescription]);
}
在 didFinishLaunchingWithOptions 中调用此方法......
{
[self copyDatabaseFile];
}
然后在代码中需要时打开文件...
答案 1 :(得分:2)
请注意此屏幕截图底部的SQLite文件...
此屏幕截图来自我正在处理的当前项目。您可以将文件存储在该位置,然后使用以下代码进行引用(将名称更改为您自己的文件)
NSBundle *myBundle = [NSBundle mainBundle];
NSString *absPath= [myBundle pathForResource:@"realdb" ofType:@"sqlite3"];
_databasePath = [[NSString alloc] initWithString: absPath];
(将此代码放在您的UIView的.m文件中)
主要基于对相关问题的回答:
Resources folder path in cocoa app
答案 2 :(得分:1)
使用此:
- (void)createOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:@"localdb.db"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL success = [fileManager fileExistsAtPath:dbPathString];
if(!success) // all the time it's YES, even if I delete database
{
// copy database from application folder
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:dbPathString];
BOOL fileExists = [fileManager fileExistsAtPath:dbPathString];
if (fileExists) {
const char *dbPath = [dbPathString UTF8String];
//creat db here
if (sqlite3_open(dbPath, &companyDB)==SQLITE_OK) {
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS storelist (StoreID NOT NULL PRIMARY KEY AUTOINCREMENT, StoreName TEXT NULL, StoreAddr TEXT NULL, StoreHost TEXT NULL, StorePort TEXT NULL, StoreUser TEXT NULL, StorePass TEXT NULL, StoreDB TEXT NULL)";
sqlite3_exec(companyDB, sql_stmt, NULL, NULL, NULL);
sqlite3_close(companyDB);
[fileManager removeItemAtPath:dbPathString error:nil];
[fileManager copyItemAtPath:databasePathFromApp toPath:dbPathString error:nil];
}}}}