iOS关于数据库操作之二 将工程里的数据库文件在程序加载时移至沙盒
来源:互联网 发布:新闻类app源码 编辑:程序博客网 时间:2024/06/10 21:20
iOS程序要访问本地数据库,就只能访问沙盒中的文件,之前我在用模拟器开发的时候,会把数据库的sql文件直接拖进模拟器的沙盒里,程序功能能够正常实现。但是如果用真机的话,这种做法是万万不可取的,因为在程序安装到机器之前,手机或者iPad上并不存在与该程序对应的沙盒,普通用户更是不可能找到这个沙盒。因此,正确的做法是:将数据库文件放在工程中,在程序第一次加载的时候,将数据库移至程序沙盒。
具体操作如下:
+(void)moveToDBFile
{ //1、获得数据库文件在工程中的路径——源路径。
NSString *sourcesPath = [[NSBundlemainBundle] pathForResource:@"database"ofType:@"sqlite"];
//2、获得沙盒中Document文件夹的路径——目的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *desPath = [documentPath stringByAppendingPathComponent:@"database.sqlite"];
//3、通过NSFileManager类,将工程中的数据库文件复制到沙盒中。
NSFileManager *fileManager = [NSFileManagerdefaultManager];
if (![fileManager fileExistsAtPath:desPath])
{
NSError *error ;
if ([fileManager copyItemAtPath:sourcesPath toPath:desPath error:&error]) {
NSLog(@"数据库移动成功");
}
else {
NSLog(@"数据库移动失败");
}
}
}
在程序中的加载的入口方法中调用该方法,即可在程序加载的时候将数据库文件添加的沙盒。然后从沙盒中读取数据库文件 .
//1、在全局中声明一个静态db对象
static FMDatabase *db = nil;
//2、获得沙盒数据库文件的路径
+(NSString *)databaseFilePath
{
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [filePath objectAtIndex:0];
NSLog(@"%@",filePath);
NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"database.sqlite"];
return dbFilePath;
}
//3、从指定沙盒中获得数据库并将对象返回给调用者
+(FMDatabase *)getDatabase
{
if (!db) {
//如果db对象不为空,从指定路径中得到数据库。这里已经要记得 retain, 否则在后面会引起程序崩溃。
db = [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]] retain];
}
returndb;
具体操作如下:
+(void)moveToDBFile
{ //1、获得数据库文件在工程中的路径——源路径。
NSString *sourcesPath = [[NSBundlemainBundle] pathForResource:@"database"ofType:@"sqlite"];
//2、获得沙盒中Document文件夹的路径——目的路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *desPath = [documentPath stringByAppendingPathComponent:@"database.sqlite"];
//3、通过NSFileManager类,将工程中的数据库文件复制到沙盒中。
NSFileManager *fileManager = [NSFileManagerdefaultManager];
if (![fileManager fileExistsAtPath:desPath])
{
NSError *error ;
if ([fileManager copyItemAtPath:sourcesPath toPath:desPath error:&error]) {
NSLog(@"数据库移动成功");
}
else {
NSLog(@"数据库移动失败");
}
}
}
在程序中的加载的入口方法中调用该方法,即可在程序加载的时候将数据库文件添加的沙盒。然后从沙盒中读取数据库文件 .
//1、在全局中声明一个静态db对象
static FMDatabase *db = nil;
//2、获得沙盒数据库文件的路径
+(NSString *)databaseFilePath
{
NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [filePath objectAtIndex:0];
NSLog(@"%@",filePath);
NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"database.sqlite"];
return dbFilePath;
}
//3、从指定沙盒中获得数据库并将对象返回给调用者
+(FMDatabase *)getDatabase
{
if (!db) {
//如果db对象不为空,从指定路径中得到数据库。这里已经要记得 retain, 否则在后面会引起程序崩溃。
db = [[FMDatabasedatabaseWithPath:[selfdatabaseFilePath]] retain];
}
returndb;
}
转载
0 0
- iOS关于数据库操作之二 将工程里的数据库文件在程序加载时移至沙盒
- iOS关于数据库操作之二 将工程里的数据库文件在程序加载时移至沙盒
- IOS开发(二)之程序首次启动时加载关于程序应用的简介
- iOS常见问题之异步加载图片在UITableView里显示时的奇异的现象
- 如何将iOS工程中的.plist文件里的内容取出在android的工程中使用
- IOS开发之程序首次启动时加载关于程序应用的简介
- IOS开发之程序首次启动时加载关于程序应用的简介
- IOS开发之程序首次启动时加载关于程序应用的简介
- 关于iOS逆向工程的工具(二)
- 小程序---从图片库里随机挑选并移至新的文件夹
- iOS开发笔记之二十九——一个关于工程目录读写权限的问题
- MySQL数据库文件的加载
- eclipse里在操作数据库前之理解
- 将raw下的数据库文件导入到本地数据库
- 关于eclipse里配置tomcat以及在tomcat里载入工程的说明
- 关于图片上传数据库,下载到picturebox里的操作。
- VC7/8的工程移至VC6时碰到的warning C4005: '_WIN32_WINDOWS' : macro redefinition及其解决
- JSP在数据库中的使用之二:数据库操作的封装与测试
- 使用total commander 复制文件名
- i++ , ++i 的区别
- ios 调用打电话,发短信的功能
- OpenStreetMap服务器搭建显著简化
- MyBatis与Spring 整合后,创建Bean报奇葩错的解决
- iOS关于数据库操作之二 将工程里的数据库文件在程序加载时移至沙盒
- 大型分布式 Java 应用
- 二、安装Git、设置Git、获取Git帮助
- Android 多媒体开发学习之加载大图片
- AndroidManifest.xml file missing!
- Linux中crontab的基本用法(Linux新手之路)
- Lua 学习备忘录2
- Ruby for Rails 学习笔记(一)
- 常用的web安装测试用例