在iOS中启动应用程序时如何复制sqlite数据库?
Posted
技术标签:
【中文标题】在iOS中启动应用程序时如何复制sqlite数据库?【英文标题】:How to copy sqlite database when application is launched in iOS? 【发布时间】:2013-03-07 15:22:02 【问题描述】:我想在每次启动应用程序时将我的 sqlite 数据库从数据库位置复制到我的 ios 应用程序的最新更新。
有什么办法吗?
【问题讨论】:
请确保对于此类问题,您确实列出了对您尝试过的资源的引用,或者只是在这里写下您的代码,以便每个人都知道您的方法/方法 【参考方案1】:您可以将以下方法添加到您的 appdelegate
- (void) copyDatabaseIfNeeded
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
- (NSString *) getDBPath
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
//NSLog(@"dbpath : %@",documentsDir);
return [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
并在您的启动方法中调用此方法
[self copyDatabaseIfNeeded];
希望这会有所帮助。
【讨论】:
不,如果您的原始数据库很脏,您希望保留用户活动数据而不是覆盖整个数据库。 新替换的数据库是否可写?我已经替换了一个数据库,但它变成了只读的。我怎样才能让它可写? 对于其他来到这里并需要表格数据的人您还需要复制.sqlite-wal
和.sqlite-shm
文件。【参考方案2】:
试试这个:
NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDocDirectory = [docPath objectAtIndex:0];
self.strDatabasePath = [strDocDirectory stringByAppendingPathComponent:YOUR_DB];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: self.strDatabasePath ] == NO)
const char *dbpath = [self.strDatabasePath UTF8String];
sqlite3 *contactDB;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS YOUR_Table (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)";
if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
NSLog(@"Failed to create table");
sqlite3_close(contactDB);
【讨论】:
【参考方案3】:在 AppDelegate 类
中试试这个.h 文件
@property(nonatomic, retain) NSString *dbPath;
.m 文件
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[self copyDatabaseIfNeeded];
- (void) copyDatabaseIfNeeded
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success)
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"sampleDB.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
/********* Database Path *********/
- (NSString *) getDBPath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"sampleDB.sqlite3"];
如果在应用程序中没有找到任何数据库,它会自动复制。
希望对你有所帮助。
谢谢。
【讨论】:
试试这个,一定能帮到你。【参考方案4】:在应用程序启动时使用以下代码处理数据库
在您的 appdelegate.m 中
在
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[self getdatabase];
return YES;
并将以下函数添加到您的 appdelegate.m
-(void)getdatabase
BOOL success;
NSFileManager *filemanager = [NSFileManager defaultManager];
NSError *error;
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *DBPath = [pathArray objectAtIndex:0];
NSString *writableDBPath = @"";
writableDBPath = [DBPath stringByAppendingPathComponent:@"xyz.sqlite"];
NSLog(@"writableDBPath:%@",writableDBPath);
success = [filemanager fileExistsAtPath:writableDBPath];
if (!success)
NSString *defaultDBpath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"xyz.sqlite"];
success = [filemanager copyItemAtPath:defaultDBpath toPath:writableDBPath error:&error];
if (!success)
NSAssert(0, @"failed to copy database at path with message '%@'.",[error localizedDescription]);
NSLog(@"111writableDBPath:%@",writableDBPath);
【讨论】:
【参考方案5】:-(void)openDatase
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
self.databasePath = [documentsDirectory stringByAppendingPathComponent:@"weightCal.sqlite"];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
NSLog(@"Database Successfully Opened ");
else
NSLog(@"Error in opening database ");
-(void)dealloc
sqlite3_close(database);
[super dealloc];
-(void)copyDatabase
BOOL success;
NSFileManager *fileManager=[NSFileManager defaultManager];
NSError *error;
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory=[paths objectAtIndex:0];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"test.sqlite"];
success = [fileManager fileExistsAtPath:writablePath];
if(success)
return;
NSString *defaultPath=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.sqlite"];
success=[fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
if(!success)
NSAssert1(0,@"Failed to create writable database file with message '%@' .",[error localizedDescription]);
在.h
中声明以下 teo 变量:
sqlite3 *database;
NSString *databasePath;
【讨论】:
【参考方案6】:这里是 Swift 4/5
func copyDatabaseIfNeeded(sourcePath : String) -> Bool
var destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
destPath = destPath + "/foo.db3"
let databaseExistsWhereNeeded = FileManager.default.fileExists(atPath: destPath)
if (!databaseExistsWhereNeeded)
do
try FileManager.default.copyItem(atPath: sourcePath, toPath: destPath)
print("db copied")
catch
print("error during file copy: \(error)")
return true
【讨论】:
我的类似代码在 Xcode 11.4 中编译良好 - 所以,是的 - 如果您遇到问题,请告诉我。 非常感谢。它似乎工作正常。我只是想知道 Swift 5 是否允许将其优化为更少的行或任何东西。感谢您的回答以上是关于在iOS中启动应用程序时如何复制sqlite数据库?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 ios 中打开一个 sqlite 文件进行阅读而不复制到文档?
如何将 PreCreated 和 Pre Populated 完整的 sqlite database.db 文件复制到 iOS 应用程序中