在不存在文件名 fileName 的情况下调用 sqlite3 fileName 时,如何避免 sqlite3 创建新的空数据库?
如果我使用
$ sqlite3 fileName
sqlite> .s
sqlite>_
并且没有名为 fileName 的文件,sqlite3 会创建一个新的空数据库。有办法避免这种情况吗?理想情况下,如果找不到文件,我希望它重试几次,然后失败并显示错误消息。像这样的事情:
$ sqlite3 --failIfNoSuchFile --retry 3 fileName
sqlite> No such file, retrying ...
sqlite> No such file, retrying ...
sqlite> No such file, retrying ...
sqlite> Aborting...
Error: No file named fileName
$_
这样的事情可能吗?
我问的原因是因为我们现有的数据库被粉碎并被这个新的空白数据库取代,只是因为网络问题阻止了 sqlite3 首先找到该文件,但并没有阻止它创建新的空数据库(并粉碎了老的)!至少,这是我们提出的最有可能的理论来解释数据库突然空了(即使结构消失了,.s
什么也没有返回,文件大小为0)
有什么想法吗?
顺便说一句,在实际上下文中,tcl 脚本连接到数据库并插入数据。问题显示为“找不到表”错误,我们发现数据库文件为空。
If I use
$ sqlite3 fileName
sqlite> .s
sqlite>_
and there is no file named fileName, sqlite3 creates a new empty database. Is there a way to avoid this? Ideally, I would like it to retry a couple of times if the file is not found, then fail with an error message. Something like this:
$ sqlite3 --failIfNoSuchFile --retry 3 fileName
sqlite> No such file, retrying ...
sqlite> No such file, retrying ...
sqlite> No such file, retrying ...
sqlite> Aborting...
Error: No file named fileName
$_
Is something like this possible?
The reason I ask is because an existing database we had was crushed and replaced by this new blank database just because a networking problem prevented sqlite3 from finding the file in the first place but did not prevent it from creating the new empty one (and crushing the old one)! At least, that's the most probable theory we've come up with to explain the sudden emptiness of the database (even the structure was gone, .s
returned nothing and the file size was 0)
Any ideas?
btw, in the actual context, a tcl script connects to the database and inserts data. The problem appeared as a "table not found" error and we found out the database file was empty..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道让 sqlite3 执行此操作的标志。您可以在调用 sqlite3 之前让 tcl 脚本检查该文件吗?
像这样的事情会起作用吗:
I'm not aware of a flag to have sqlite3 do this. Could you just have you tcl script check for the file before calling sqlite3?
Would something like this work:
自最初的问题 & 以来的时间答案已发布,SQLite 添加了
mode
URI 文件名参数。设置mode=rw
会阻止创建数据库文件(如果该文件尚不存在):In the time since the original question & answer were posted, SQLite has added the
mode
URI filename parameter. Settingmode=rw
prevents the creation of a database file if it does not already exist: