什么原因导致在SQLite中创建日志文件? [英] What causes a journal file to be created in SQLite?

查看:947
本文介绍了什么原因导致在SQLite中创建日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始注意到我的iPhone应用程序的SQLite查询有一种奇怪的行为。每当我执行INSERT语句时,都会在我的db文件旁边创建一个日志文件(确切的文件名是userdata.db-journal)。

I started noticing a weird behavior with my SQLite queries for my iPhone application. Whenever I execute an "INSERT" statement, a journal file is created beside my db file (the exact filename is "userdata.db-journal").

如果我理解的话正确的文档,SQLite使用此日志文件,以便在操作失败时能够回滚。但是文件在操作完成后仍保留在那里。

If I understand the docs correctly, this journal file is used by SQLite to be able to rollback in case the operation fails. But the file stays there after the operation is completed.

我执行准备,绑定和执行查询的经典步骤(为简单起见,不检查返回值):

I do the classic steps for preparing, binding and executing the query (no checks for return values for simplicity):

sqlite3 *db = NULL;
sqlite3_open( "userdata.db", &db );

sqlite3_stmt *insertStmt = NULL;
const char *query = "INSERT INTO table VALUES(?,?)";
sqlite3_prepare_v2( db, query, -1, &insertStmt, NULL );

sqlite3_bind_int( insertStmt, 1, 0 );
sqlite3_bind_int( insertStmt, 2, 0 );

sqlite3_step( insertStmt );

sqlite3_reset( insertStmt );
sqlite3_clear_bindings( insertStmt );

sqlite3_close( db );

日志文件是在调用'sqlite3_step'函数后创建的,并保持不变直到我关闭数据库。我需要做些什么才能让它消失?

The journal file is created after the 'sqlite3_step' function is called and stays there until I close the db. What do I need to do to make it go away?

推荐答案

SQLite默认情况下会在事务开始时创建日志文件,这是一件好事。通常它会在提交时删除文件。

SQLite by default creates the journal file at the start of a transaction, which is a good thing. Normally it deletes the file upon commit.

您可以通过PRAGMA sql命令更改此行为以简单地截断文件(无需创建/删除)或将其归零。并非所有选项都支持您的SQLite版本。

You can change this behavior to simply truncate the file (no create/delete necessary) or to zero it, via a PRAGMA sql command. Not all options may be supported on your version of SQLite.

PRAGMA journal_mode = DELETE | TRUNCATE | PERSIST | MEMORY | OFF  

写入操作通常直到COMMIT才会完成,这是通过截断,删除或无效来实现的日记。

Write operations are normally not completed until you COMMIT, which is implemented by truncating, deleting, or invalidating the journal.

如果这个行为是新的,我没有解释为什么它可能已经改变,除了SQLite的升级改变了默认行为,或者已经变得更多每次交易都不会重新打开期刊。

If this behavior is new, I have no explanation for why it might have changed, other than perhaps an upgrade of SQLite changed default behavior, or has become more sophisticated by not reopening the journal for every transaction.

这篇关于什么原因导致在SQLite中创建日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆