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

1、得到沙盒中的数据库文件夹

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

方法2:数据库

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


sqlite3 *_db=NULL;sqlite

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

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

2、数据库的导入

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

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.sqlitesqlite3