解决Sqlite在IOS应用中使用方法案例实现

移动开发 iOS
Sqlite在IOS应用中如何应用是本文要介绍的内容,主要是来了解IOS应用中sqlite的使用方法,具体内容的实现,来看本文详细代码。

SqliteIOS应用中如何应用是本文要介绍的内容,主要是来了解IOS应用sqlite的使用方法,sqlite数据库初始化,复制到用户目录,并判断是否数据库已经存在,或者复制是否成功!

在AppDelegate.m中输入以下代码,以便复制预置数据库到指定doucment目录

  1. - (BOOL) initializeDb {  
  2. NSLog (@"initializeDB");  
  3. // look to see if DB is in known location (~/Documents/$DATABASE_FILE_NAME)  
  4. //START:code.DatabaseShoppingList.findDocumentsDirectory  
  5.  
  6. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  7.  
  8. NSString *documentFolderPath = [searchPaths objectAtIndex: 0];  
  9. //查看文件目录  
  10. NSLog(@"%@",documentFolderPath);  
  11. dbFilePath = [documentFolderPath stringByAppendingPathComponent:@"shopping.db"];  
  12. //END:code.DatabaseShoppingList.findDocumentsDirectory  
  13. [dbFilePath retain];  
  14. //START:code.DatabaseShoppingList.copyDatabaseFileToDocuments  
  15. if (! [[NSFileManager defaultManager] fileExistsAtPath: dbFilePath]) {  
  16. // didn't find db, need to copy  
  17. NSString *backupDbPath = [[NSBundle mainBundle] pathForResource:@"shopping" ofType:@"db"];  
  18. if (backupDbPath == nil) {  
  19. // couldn't find backup db to copy, bail  
  20. return NO;  
  21. } else {  
  22. BOOL copiedBackupDb = [[NSFileManager defaultManager] copyItemAtPath:backupDbPath toPath:dbFilePath error:nil];  
  23. if (! copiedBackupDb) {  
  24. // copying backup db failed, bail  
  25. return NO;  
  26. }  
  27. }  
  28. }  
  29. return YES;  
  30. //END:code.DatabaseShoppingList.copyDatabaseFileToDocuments  
  31. NSLog (@"bottom of initializeDb");  
  32. }  
  33. - (void)applicationDidFinishLaunching:(UIApplication *)application {  
  34. // copy the database from the bundle if necessary  
  35. if (! [self initializeDb]) {  
  36. // TODO: alert the user!  
  37. NSLog (@"couldn't init db");  
  38. return;  
  39. }  
  40.     // Add the tab bar controller's current view as a subview of the window  
  41.     [window addSubview:tabBarController.view];  

小结:解决SqliteIOS应用中使用方法案例实现的内容介绍完了,希望通过本文能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2012-03-06 10:17:45

iOS SQLite3iOSSQLite3

2011-05-17 16:20:46

C++

2011-03-30 10:41:11

C++数据库

2010-11-19 09:56:38

SQLiteAndroid

2012-12-27 13:04:17

Android开发SQLite数据库

2023-01-28 17:41:07

Java代码

2011-09-02 19:12:59

IOS应用Sqlite数据库

2012-02-13 14:10:11

MonoTouchiOS应用Visual Stud

2012-02-13 14:22:22

MonoTouchiOS应用Visual Stud

2011-07-29 11:10:56

IOS SimpleLogg 日志

2011-08-19 10:01:09

iPhone应用SqliteUITableView

2013-07-25 14:12:53

iOS开发学习UITableView

2021-02-25 11:19:37

谷歌Android开发者

2011-08-10 16:08:02

iPhoneProtocol协议

2012-01-18 14:14:29

iOS教程iOS5

2018-06-20 10:34:56

堆栈iOSswift

2023-12-22 09:11:45

AndroidNFC移动开发

2011-08-31 16:30:19

Lua多线程

2011-09-06 16:30:32

iOS系统静态链接库

2009-09-22 12:17:59

ibmdwLotus
点赞
收藏

51CTO技术栈公众号