[英]Pull Database from Documents Directory
我一直在使用以下代码从应用程序的支持文件中提取sqlite数据库。 一切正常,没有任何问题。
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"TulsaListBars.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}else{
NSLog(@"File Exists: %@", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "SELECT * FROM Locations WHERE bar = 'yes'";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement: %s", sqlite3_errmsg(db));
}
我没有从支持文件中删除数据库并放入服务器中。 我正在使用以下代码将数据库从服务器拉到我的文档目录中。 我已经验证了该文件即将到达“文档目录”,并且对于所有表都是正确的。 但是,我现在收到错误消息“ prepare语句有问题:没有这样的表:Locations”
NSURL *url = [NSURL URLWithString:@"my web site/TulsaListBars.sqlite"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSString *appDocDir = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [appDocDir stringByAppendingPathComponent:@"TulsaListBars.sqlite"];
[data writeToFile:storePath atomically:TRUE];
现在数据库不在我的文档目录中,而不是在支持文件中,这是一个问题吗?
更新:我已经重新启动并重置了模拟器,现在看来数据库没有被加载到Documents Directory中。 任何帮助,将不胜感激。
dataWithContentsOfURL正在同步下载数据库文件,并且可能无法获取整个文件。
将方法更改为与NSURLConnection异步,并使用委托方法didReceiveData追加到NSMutableData对象,并使用connectionDidFinishLoading来使用该对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.