回答
您可以添加下列方法將的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];
希望這將有助於調用此方法在你沒有完成。
感謝ü... 它的工作原理 – 2013-03-19 04:26:31
不,如果您的原始數據庫很髒,您希望保留用戶活動數據而不是覆蓋整個數據庫。 – OMGPOP 2013-06-06 14:58:52
新更換的數據庫是否可寫?我已經替換了一個數據庫,但它變成了只讀。我怎樣才能使它可寫? – 2017-03-14 08:00:20
試試這個:
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);
在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"];
}
It'l l自動複製,如果在應用程序中沒有找到任何數據庫。
希望它能幫助你。
謝謝。
試試這個,它一定會幫助你。 – 2013-03-19 04:08:03
thnks ..其工作很好.... – 2013-03-19 04:26:01
好的。好..享受編碼.. :) – 2013-03-19 04:26:23
下面的代碼使用了應對數據庫應用程序啓動時
在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);
}
謝謝...它對我很好! – 2013-03-19 04:16:04
-(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]);
}
}
聲明TEO變量按照.h
:
sqlite3 *database;
NSString *databasePath;
- 1. 當更新應用程序時移動ios sqlite數據庫
- 2. 如何在安裝android應用程序時複製sqlite數據庫
- 3. 在應用程序重啓時重新創建SQLite數據庫
- 4. 在Android數據庫中重新啓動應用程序時重複的數據
- 5. 在iPhone iOS 5應用程序中使用SQLite數據庫?
- 6. 用戶啓動應用程序時用臨時文件填充sqlite數據庫
- 7. 如何將SQLite數據庫添加到iOS(CoreData)應用程序?
- 8. 如何在程序啓動時啓動mysql數據庫?
- 9. 在現有iOS應用程序中更新sqlite數據庫
- 10. 重新啓動應用程序後刪除SQLite數據庫
- 11. 如何在應用程序退出時清除SQLite數據庫?
- 12. iOS應用程序在啓動時
- 13. Apple Watch應用程序在iOS應用程序啓動時獲取數據
- 14. 如何在啓動時啓動控制檯應用程序(!)? C#
- 15. 如何在PhoneGap應用程序中使用SQLite數據庫?
- 16. 啓動時如何啓動iOS應用程序?
- 17. 從數據庫啓動應用程序
- 18. iOS:在第一次啓動應用程序時管理數據
- 19. 在iOS設備中啓動應用程序時Unity3d iPhone SQLite數據庫爲空設備
- 20. 如何在ionic2應用程序中預覽SQLITE數據庫
- 21. 如何在Android應用程序中包含Sqlite數據庫?
- 22. 如何在Adobe AIR應用程序中重置sqlite數據庫
- 23. 如何在黑莓應用程序中包含SQLite數據庫?
- 24. 如何在sqlite數據庫中部署android應用程序?
- 25. iOS中的SQLite數據庫應用程序和表ID
- 26. iOS中的Sqlite數據庫修改和應用程序更新
- 27. 如何在Android應用程序中使用已製作的SQLite數據庫
- 28. mybatis在應用程序啓動時創建/更新數據庫?
- 29. 在Android中寫入SQLite數據庫時,應用程序崩潰
- 30. 在android中創建SQlite數據庫時應用程序freeezes
請,請確保這樣的問題,你做你嘗試過什麼來源或只是在你的代碼寫在這裏列表的引用,這樣每個人都可以知道尤爾方法/ – madLokesh 2013-07-24 11:59:07