Xcode 中sqlite数据库的创建与数据库的导入

来源:互联网 发布:林弯弯的淘宝店叫什么 编辑:程序博客网 时间:2024/05/22 04:37

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

方法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



0 0
原创粉丝点击