0

我一直在使用以下代码从我的应用程序的支持文件中提取一个 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));
}

我没有从支持文件中删除数据库并放入服务器。我正在使用以下代码将数据库从服务器拉到我的文档目录中。我已经验证该文件正在进入文档目录并且所有表格都是准确的。但是,我现在收到错误“准备语句问题:没有这样的表:位置”

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];

这是数据库现在在我的文档目录而不是支持文件中的问题吗?

更新:我已经重新启动并重置了模拟器,现在看来数据库没有被加载到文档目录中。任何帮助,将不胜感激。

4

1 回答 1

0

dataWithContentsOfURL 正在同步下载数据库文件,可能无法获取整个文件。

使用 NSURLConnection 将方法更改为异步,并使用委托方法didReceiveData附加到 NSMutableData 对象和connectionDidFinishLoading以使用该对象。

于 2012-08-04T09:03:24.633 回答