Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone SQLite问题-数据库路径_Iphone_Sqlite - Fatal编程技术网

Iphone SQLite问题-数据库路径

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

我正在尝试从数据库中选择数据,我有以下代码: 代码:

我在第一个IF语句中遇到EXC_BAD_访问错误。任何人都有任何想法

NSLOG文档中的DIR和databasePath也为空

我在ViewDidLoad中有以下代码:

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