项目中使用sqlite

需求是:

需求是:项目的数据操作都是在本地完成,然后有统一的接口来进行同步,然后用Navicat for SQLite 创建好表格和数据库后,放到项目的Document文件中,这里主要干货是怎么处理NSBundle中的文件。


一、获得沙盒中的数据库文件夹

方法1:NSString *path=[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]stringByAppendingPathComponent:@"dlg.sqlite"];

方法2:

NSString *path=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"dlg.sqlite"];


sqlite3 *_db=NULL;

int result=sqlite3_open(path.UTF8String, &_db);//如果数据库文件不存在时,会自动创建。

if (result==SQLITE_OK) {NSLog(@"成功打开数据库");}

二、数据库的导入

操作步骤:1、把工程外的数据库文件拖入该工程中(比如该数据库文件名为otherDlg.sqlite

2NSString *path=[[NSBundle mainBundle]pathForResource:@"otherDlg"ofType:@"sqlite"];

NSLog(@"%@",path);//系统自动查找改文件名所在的路径

特别注意的是,工程文件夹中的otherDlg.sqlite文件不会被操作,而是在path路径下的otherDlg.sqlite文件会被操作,就好比该工程文件夹下的otherDlg.sqlite被复制到path路径中,数据库的操作都和path中的otherDlg.sqlite有关,比如在数据库中创建了一个表,该表不会显示在工程文件夹下的otherDlg.sqlite中而会显示在path路径中的otherDlg.sqlite




文章来自于:http://blog.csdn.net/duliangang001/article/details/46460621

猜你喜欢

转载自blog.csdn.net/small_years/article/details/78333190