无法从SQLite数据库中检索值

时间:2011-11-10 03:15:33

标签: objective-c ios xcode sqlite

我正在开发一个iPad应用程序 我的资源文件夹中有一个SQLite数据库 当我的应用程序启动时,这些代码会将数据库复制到我的NSDocumentDirectory。

- (void) copyDatabaseIfNeeded {
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error;
  NSString *dbPath = [self getDBPath];
  BOOL success = [fileManager fileExistsAtPath:dbPath];

  if(!success) {

    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Mydata.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];

    if (!success)
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
  }
}

- (NSString *) getDBPath {
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
  NSString *documentsDir = [paths objectAtIndex:0];
  return [documentsDir stringByAppendingPathComponent:@"Mydata.sqlite"];
}

在我的应用程序的另一个视图中,我有这些代码来打开数据库并检索一些值。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
if(sqlite3_open([[documentsDir stringByAppendingPathComponent:@"Mydata.sqlite"] UTF8String], &db) != SQLITE_OK){
    sqlite3_close(db);
    NSAssert(0, @"Failed to open database.");
}
NSString *sql = [[NSString alloc] initWithFormat:@"SELECT HOMEONE, HOMETWO, HOMETHREE, HOMEFOUR FROM STUDENTS WHERE NAME='%@'",Name];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK)
{
    while(sqlite3_step(statement) == SQLITE_ROW){
        char *one = (char *) sqlite3_column_text(statement,0);
        ActionOne = [[NSString alloc] initWithUTF8String:one];
        char *two = (char *) sqlite3_column_text(statement,1);
        ActionTwo = [[NSString alloc] initWithUTF8String:two];
        char *three = (char *) sqlite3_column_text(statement,2);
        ActionThree = [[NSString alloc] initWithUTF8String:three];
        char *four = (char *) sqlite3_column_text(statement,3);
        ActionFour = [[NSString alloc] initWithUTF8String:four];
    }
    sqlite3_finalize(statement);
}

是的,使用这些代码,我可以从数据库中检索值 但现在我需要检索更多的值,所以我编辑了我的代码。

NSString *sql = [[NSString alloc] initWithFormat:@"SELECT HOMEONE, HOMETWO, HOMETHREE, HOMEFOUR, VOICEONE, VOICETWO, VOICETHREE, VOICEFOUR FROM STUDENTS WHERE NAME='%@'",Name];
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK)
{
    while(sqlite3_step(statement) == SQLITE_ROW){
        char *one = (char *) sqlite3_column_text(statement,0);
        ActionOne = [[NSString alloc] initWithUTF8String:one];
        char *two = (char *) sqlite3_column_text(statement,1);
        ActionTwo = [[NSString alloc] initWithUTF8String:two];
        char *three = (char *) sqlite3_column_text(statement,2);
        ActionThree = [[NSString alloc] initWithUTF8String:three];
        char *four = (char *) sqlite3_column_text(statement,3);
        ActionFour = [[NSString alloc] initWithUTF8String:four];

        char *vone = (char *) sqlite3_column_text(statement,4);
        VoiceOne = [[NSString alloc] initWithUTF8String:vone];
        char *vtwo = (char *) sqlite3_column_text(statement,5);
        VoiceTwo = [[NSString alloc] initWithUTF8String:vtwo];
        char *vthree = (char *) sqlite3_column_text(statement,6);
        VoiceThree = [[NSString alloc] initWithUTF8String:vthree];
        char *vfour = (char *) sqlite3_column_text(statement,7);
        VoiceFour = [[NSString alloc] initWithUTF8String:vfour];
    }
    sqlite3_finalize(statement);
}

现在它甚至没有检索到HOMEONE,HOMETWO,HOMETHREE,HOMEFOUR值。 我尝试更改我的SQL语句以仅检索VOICEONE,VOICETWO,VOICETHREE,VOICEFOUR值,它也没有给我任何值。

我做了一些检查,发现应用程序甚至没有进入这一行:

    if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)== SQLITE_OK)

这意味着sqlite3_prepare_v2不成功? 我检查了数据库字段类型 我尝试在“SQLite数据库浏览器”中执行SQL语句,并且能够返回值。

我可以知道我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

可能会出现此问题,因为您可能更改了数据库并复制了新数据库并忘记从模拟器或设备中删除应用程序并尝试运行该项目。我建议如果你在模拟器中运行它然后plz复制数据库路径并从这个路径在SQLite数据库浏览器中打开它并尝试在这里运行你的sql语句,你会发现它也没有在这里运行。如果将新数据库复制到项目中,请从模拟器或设备中删除应用程序。