问题描述
我有一个sqlite数据库,正在为其添加新单词.问题是,仅在重新启动应用程序后,我才能看到它们已添加到表中.重新启动应用程序之前,"SELECT"语句不会看到"新添加的元素.
I have a sqlite database and I am adding to it new words. The problem is that I can see them added to a table only after restarting application. The "SELECT" statement doesn't "see" newly added elements before restarting application.
为什么会这样?
我正在创建某种字典.这是我添加新项目的方法:
I am creating some kind of a dictionary. Here is how I add new items:
const char *sql_query = "INSERT INTO words(word) VALUES(?)";
if(sqlite3_prepare_v2(database, sql_query, -1, &addWordsStmt, NULL) != SQLITE_OK)
{
return FALSE;
}
sqlite3_bind_text(addWordsStmt, 1, [ word UTF8String], -1, SQLITE_TRANSIENT);
if( sqlite3_step(addWordsStmt) != SQLITE_DONE)
NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
sqlite3_reset(addWordsStmt);
sqlite3_finalize(addWordsStmt);
这是我的检索代码:
const char *sql_query = "SELECT word FROM words WHERE id=?";
if(sqlite3_prepare_v2(database, sql_query, -1, &getWordsStmt, NULL) != SQLITE_OK)
{
return;
}
sqlite3_bind_int(getWordsStmt, 1, wordid);
if( sqlite3_step(getWordsStmt) != SQLITE_ROW)
{
NSLog(@"Error while getting data. '%s'", sqlite3_errmsg(database));
sqlite3_reset(getWordsStmt);
return;
}
NSString *word = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getWordsStmt, 0)];
sqlite3_reset(getWordsStmt);
sqlite3_finalize(getWordsStmt);
推荐答案
我的猜测是您正在进行交易.在打开和关闭例程期间,必须有一些其他调用将这些调用包装在事务中,并且不会显示在代码片段中.
My guess is that you are in a transaction. There must be some other calls during your open and close routines that are wrapping your calls in a transaction and not displayed in your code fragments.
这就是为什么在应用程序退出之前看不到新单词的原因
That is why you don't see the new words until you application exits
这篇关于SQLite数据库-无法看到更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!