Sqlite数据库加载失败-Sqlite准备语句出现问题-iPhone-xCode 4.3.1
我对加载SQLite数据库的以下代码有问题Sqlite数据库加载失败-Sqlite准备语句出现问题-iPhone-xCode 4.3.1,iphone,sql,database,xcode,sqlite,Iphone,Sql,Database,Xcode,Sqlite,我对加载SQLite数据库的以下代码有问题 - (NSArray *)getDatabase { NSLog(@"Get Database Called"); NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease]; NSString *query = @"SELECT Description, UniqueID, HexCoords, NoHexCoords, NoAnnots, AnnotT
- (NSArray *)getDatabase {
NSLog(@"Get Database Called");
NSMutableArray *retval = [[[NSMutableArray alloc] init] autorelease];
NSString *query = @"SELECT Description, UniqueID, HexCoords, NoHexCoords, NoAnnots, AnnotText, DescriptionFormatting, HexCoordsPortrait FROM MainTable";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil)
== SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *nameChars = (char *) sqlite3_column_text(statement, 0);
char *desChars = (char *) sqlite3_column_text(statement, 1);
int uniqueID = sqlite3_column_int(statement, 2);
通过使用断点,我可以看到问题出在if语句上,代码永远不会通过这个if语句。有人能发现可能出了什么问题吗?代码在几个月前就开始工作了,我最近升级到了xCode 4.3,这可能是问题所在吗
提前谢谢。是的,我同意约阿希姆的观点。有时数据库无法真正连接时会出现问题。我做的是几件事。首先,我在我的应用程序代理中添加以下代码
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyDB.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
在ApplicationIDFinishLaunching中调用此函数
现在移除当前在您的bundle中的数据库。(确保你有它的备份)。并且(如果可能的话,删除Iphone模拟器文件夹中的所有项目),因为有时会附加上以前的数据库。
清理您的项目,将数据库添加到您的包中。编译它
让我知道它是否有效
获取路径函数
- (NSString *) getDBPath {
//Search for standard documents using NSSearchPathForDirectoriesInDomains
//First Param = Searching the documents directory
//Second Param = Searching the Users directory and not the System
//Expand any tildes and identify home directories.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"MYDB.sqlite"];
}
我能看到这种行为的唯一原因是数据库设置不正确。打开数据库的调用是否可能失败?另外,最后一个参数应该是
NULL
,而不是nil
,因为该参数不是指向对象的指针。这两个函数当前都编译为0,因此不会引起问题,但我不知道有什么保证它们会保持相同。谢谢-但是“getDBPath”方法是什么?很抱歉,我忘了提到我要添加的另一件事。。在问题中的函数u中,将返回类型设置为(NSArray*),这不是最佳做法。。你应该使用NSMutableArrayTanks——这帮助我找到了问题所在——在Lion下,我现在似乎需要从实际构建文件夹中删除数据库,清理项目,然后将其拖回项目中。非常好的工作苹果-你已经使xCode 4.3.1在这方面比以前更容易出错。我不认为这是xCode的问题。我目前使用的是Xcode 4.0.2,我也遇到了同样的问题。然后花半天的时间来做。我很高兴它帮助了你