我正在尝试从数据库中选择数据,并且我有以下代码: 代码:
// Setup the database object
sqlite3 *database;
// Init the animals Array
list = [[NSMutableArray alloc] init];
NSLog(@"documents path: ", documentsDir);
NSLog(@"database path: ", databasePath);
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select route_name from Route";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Read the data from the result row
NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
// Add the animal object to the animals Array
//[list addObject:animal];
[list addObject:aName];
//[animal release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
我在第一个IF语句中收到EXC_BAD_ACCESS错误。任何人的想法。
同样在NSLOG documentsDir和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];
答案 0 :(得分:1)
看起来似乎没有保留databasePath,因此在运行视图和运行数据库代码之间,它已经被释放了。如果你想保留这些变量,你需要将最后两行更改为:
documentsDir = [[documentPaths objectAtIndex:0] retain];
databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] retain];
由于您在viewDidLoad
中执行此操作,因此您应该在viewDidUnload
中释放它们(在此方法中也设置为nil)和dealloc
。