0

sqlite データベースを使用する 1 つのアプリケーションを作成しました。最初に、シミュレーターでテストしました。シミュレーターで正常に動作し、ドキュメント ディレクトリのパスが表示されました。

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」

有効なテーブル名を使用してください。

データを挿入しようとしているテーブルに DB があるようですが、参照元の DB は古いです。あなたがする必要があるのは、XCode から DB を選択し、press DELETE Key選択してRemove Reference、もう一度 DBを追加し、シミュレーターからそれをクリア (削除) することAppです。そしてまた走る!

これで問題が解決する場合があります。

于 2012-09-06T07:39:49.703 に答える