代码之家  ›  专栏  ›  技术社区  ›  V-Xtreme

Sqlite数据库无法在设备上运行

  •  0
  • V-Xtreme  · 技术社区  · 13 年前

    我创建了一个使用sqlite数据库的应用程序。第一次我在模拟器上测试了它。它在模拟器上运行得很好,它显示了文档目录的路径:

    Library/Application support/iPhone simulator/5.1/Application/UUID/document/database.sqlite
    

    当我把它放入设备时,它显示以下路径:

    /var/mobile/Applications/60534394-66B0-4A83-BAFE-5F48FBD17FF6/Documents/database.sqlite
    

    它显示出警告:

    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Error while inserting data. 'no such table: tableName'
    

    克服这个问题的方法是什么?我应该复制应用程序目录中的数据库文件吗?

    2 回复  |  直到 13 年前
        1
  •  0
  •   Jerry Thomsan    13 年前

    您可以使用以下代码:

    - (void)SaveAction {
    
    NSString * databasefile = [[NSBundle mainBundle] pathForResource:@\"SavedPapers\" ofType:@\"db\"];
    
    sqlite3 *database = NULL;
    
    // Open the database and return a database identifier variable
    if (sqlite3_open([databasefile UTF8String], &database) == SQLITE_OK) {
    // Create and prepare template insert statements (one for each field we want to save)
    const char * SaveCommand = \"insert into papers values(?, ?, ?, ?)\";
    
    if ( sqlite3_prepare_v2(database, SaveCommand, -1, &SaveCommand_stmt, NULL) != SQLITE_OK) {
    NSAssert1(0, @\"Error while creating save statement. '%s'\", sqlite3_errmsg(database));
    }
    // Insert the data into the save statement
    sqlite3_bind_text(SaveCommand_stmt,1,[currentTitle UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(SaveCommand_stmt,2,[currentAuthor UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(SaveCommand_stmt,3,[currentSummary UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(SaveCommand_stmt,4,[currentLink UTF8String], -1, SQLITE_TRANSIENT);
    
    // Execute the SQLITE command
    if (SQLITE_DONE != sqlite3_step(SaveCommand_stmt)) {
    NSAssert1(0, @\"Error while saving data. '%s'\", sqlite3_errmsg(database));
    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@\"Save\" message:@\"Error saving  paper to database... :(\" delegate:self cancelButtonTitle:@\"OK\" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    } else {
    UIAlertView * GoodAlert = [[UIAlertView alloc] initWithTitle:nil message:@\"Paper Saved!\" delegate:self cancelButtonTitle:@\"OK\" otherButtonTitles:nil];
    [GoodAlert show];
    [GoodAlert release];
    }
    // Reset the command contents and close the database
    sqlite3_reset(SaveCommand_stmt);
    sqlite3_close(database);
    }
    

    我认为希望是有用的。

        2
  •  -2
  •   Hemang    13 年前

    * 由于未捕获的异常“NSInternalConsistencyException”而终止应用,原因:“插入数据时出错。”没有这样的表:tableName'

    请使用有效的表名。

    看起来您已经为试图插入数据的表创建了DB,但引用的DB是旧的。您需要做的是从XCode中选择您的DB, press DELETE Key 并选择 Remove Reference , 现在再次添加您的数据库 , 也清除(DELETE) App 来自模拟器 。然后再跑!

    这可能会解决你的问题。