さて、この質問にどのように名前を付けるか正確にはわかりませんが、私の問題が何であるかを説明できます。私はウェブブラウザを作成しており、sqlite データベースを使用して履歴を保存しています。ページが私のメソッドの読み込みを終了したとき:
-(void)addHistory:(NSString*)title address:(NSString*)url{
sqlite3_stmt *statement;
const char *dbpath = [_databasePath UTF8String];
if (sqlite3_open(dbpath, &_historyDB) == SQLITE_OK)
{
NSString *updateSQL = [NSString stringWithFormat:
@"UPDATE HISTORY SET title='%@' WHERE url='%@'", title, url];
const char *update_stmt = [updateSQL UTF8String];
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(_historyDB, update_stmt, -1, &stmt, NULL)==SQLITE_OK) {
NSLog(@"Updated");
}else{
NSString *insertSQL =
[NSString stringWithFormat:
@"INSERT INTO HISTORY (title, url, visits, date, search) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\")",
title, url, @"todo", @"todo", [NSString stringWithFormat:@"%@ %@", title, url]];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(_historyDB, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Added Entry");
} else {
NSLog(@"Coundn't Add Entry");
}
sqlite3_finalize(statement);
}
sqlite3_close(_historyDB);
}
}
更新が失敗した場合 (URL がテーブルに存在しないため)、else ステートメントに移動してキーを追加すると想定しました。この背後にある主なアイデアは、Web ページのタイトルを更新することです。それが機能したら、最後にアクセスした、閲覧した回数などの他のフィールドを更新します。
さまざまな方法を試してみましたが、望ましい結果が得られないようです。