DBHelper.m 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. //
  2. // DBHelper.m
  3. // ZXFrameWork
  4. //
  5. // Created by 智享 on 14-8-18.
  6. // Copyright (c) 2016年 智享. All rights reserved.
  7. //
  8. #import "DBHelper.h"
  9. #import "LKDBUtils.h"
  10. @implementation DBHelper
  11. +(FMDatabase *)openDatabase{
  12. //paths: ios下Document路径,Document为ios中可读写的文件夹
  13. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  14. NSString *documentDirectory = [paths objectAtIndex:0];
  15. NSLog(@"%@",documentDirectory);
  16. //dbPath: 数据库路径,在Document中。
  17. NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"FreeMusic.db"];
  18. // NSString *dbPath = [[NSBundle mainBundle]pathForResource:@"Test" ofType:@"db"];
  19. //创建数据库实例 db 这里说明下:如果路径中不存在"Test.db"的文件,sqlite会自动创建"Test.db"
  20. FMDatabase *db= [FMDatabase databaseWithPath:dbPath] ;
  21. if (![db open]) {
  22. NSLog(@"Could not open db.");
  23. return nil;
  24. }else{
  25. return db;
  26. }
  27. }
  28. @end