Iphone SQLite问题-数据库路径
我正在尝试从数据库中选择数据,我有以下代码: 代码: 我在第一个IF语句中遇到EXC_BAD_访问错误。任何人都有任何想法 NSLOG文档中的DIR和databasePath也为空 我在ViewDidLoad中有以下代码:Iphone SQLite问题-数据库路径,iphone,sqlite,Iphone,Sqlite,我正在尝试从数据库中选择数据,我有以下代码: 代码: 我在第一个IF语句中遇到EXC_BAD_访问错误。任何人都有任何想法 NSLOG文档中的DIR和databasePath也为空 我在ViewDidLoad中有以下代码: -(void)viewDidLoad { // Setup some globals databaseName = @"xxxxxxCoreData.sqlite"; // Get the path to the documents directory and a
-(void)viewDidLoad
{
// Setup some globals
databaseName = @"xxxxxxCoreData.sqlite";
// Get the path to the documents directory and append the databaseName
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentsDir = [documentPaths objectAtIndex:0];
documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
看起来databasePath没有被保留,所以在加载视图和运行数据库代码之间,它已经被释放了。如果要保留这些变量,需要将最后两行更改为:
documentsDir = [[documentPaths objectAtIndex:0] retain];
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain];
由于您在
viewdiload
中执行此操作,您应该在viewdiload
中释放它们(在该方法中也设置为nil)和dealloc
发布NSLog的输出(“%@”,databasePath)
看起来documentsDir有问题,这里发生了EXC_BAD_访问错误,控制台中没有显示任何内容。我稍微修改了代码,现在得到了数据库路径,但它不会执行以下IF语句:IF(sqlite3_open([databasePath UTF8String],&database)=SQLITE_OK)
documentsDir = [[documentPaths objectAtIndex:0] retain];
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain];