0

我创建了一个使用 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'

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

4

2 回答 2

0

您可以使用以下代码:

- (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);
}

我认为希望是有用的。

于 2012-09-06T08:09:32.957 回答
-2

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

使用有效的表名。

似乎您已经使用要插入数据的表进行了数据库操作,但引用数据库已旧。您需要做的是从 XCode 中选择您的 DB,press DELETE Key然后选择Remove Reference现在再次添加您的 DB,同时从模拟器中清除(删除)App。然后再跑!

这可能会解决您的问题。

于 2012-09-06T07:39:49.703 回答