Replacing SQLite database while accessing it

本文探讨了在客户端使用SQLite数据库的过程中,是否可以实现热替换数据库文件而不关闭正在运行的应用程序。文章详细讨论了在Linux和Windows环境下进行数据库文件替换的可能性及潜在风险。

[http://stackoverflow.com/questions/5536362/replacing-sqlite-database-while-accessing-it/5594127#5594127]

I am completely new to SQLite and I intend to use it in a M2M / client-server environment where a database is generated on the server, sent to the client as a file and used on the client for data lookup.

The question is: can I replace the whole database file while the client is using it at the same time?

The question may sound silly but the client is a Linux thin client and to replace the database file a temporary file would be renamed to the final file name. In Linux, a program which has still open the older version of the file will still access the older data since the old file is preserved by the OS until all file handles have been closed. Only new open()s will access the new version of the file.

So, in short:

  • client randomly accesses the SQLite database
  • a new version of the database is received from the server and written to a temporary file
  • the temporary file is renamed to the SQLite database file

I know it is a very specific question, but maybe someone can tell me if this would be a problem for SQLite or if there are similar methods to replace a database while the client is running. I do notwant to send a bunch of SQL statements from the server to the client to update the database.

share improve this question
 
1 
Any words of wisdom after another 3 years? I'm also specifically interested in the case where the client has read-only access. –  EML  Jul 2 '14 at 8:24
1 
I monitor the database file and when I notice an inode change, I close and reopen the database. Works fine. Please note that the database is not being heavily (a few queries here and there based on user interaction). –  Udo G  Jul 2 '14 at 8:57

1 Answer

up vote 1 down vote accepted

No, you cannot just replace an open SQLite3 DB file. SQLite will keep using the same file descriptor (or handle in Windows-speak), unless you close and re-open your database. More specifically:

  • Deleting and replacing an open file is either useless (Linux) or impossible (Windows). SQLite will never get to see the contents of the new file at all.

  • Overwriting an SQLite3 DB file is a recipe for data corruption. From the SQLite3 documentation:

    Likewise, if a rogue process opens a database file or journal and writes malformed data into the middle of it, then the database will become corrupt.

    Arbitrarily overwriting the contents of the DB file can cause a whole pile of issues:

    • If you are very lucky it will just cause DB errors, forcing you to reopen the database anyway.
    • Depending on how you use the data, your application might just crash and burn.
    • Your application may try to apply an existing journal on the new file. Sounds painful? It is!
    • If you are really unlucky, the user will just get back invalid results from any queries.

The best way to deal with this would be a proper client-server implementation where the client DB file is updated from data coming from the server. In the long run that would allow for far more flexibility, while also reducing the bandwidth requirements by sending updates, rather than the whole file.

If that is not possible, you should update the client DB file in three discrete steps:

  • Send a message to the client application to close the DB. This allows the application to commit any changes, remove any journal files and clean-up its internal state.
  • Replace/Overwrite the file.
  • Send a message to the client application to re-open the DB. You would have to setup all prepared statements again, though.

If you do not want to close the DB file for some reason, then you should have your application - or even a separate process - update the original DB file using the new file as input. The SQLite3 backup API might be of interest to you in that case.

share improve this answer
 
1 
Thanks for your response. You say it's useless under Linux. So when I replace the database file while the client is still accessing it does not harm (ie. the client uses just a single file descriptor for a database file). My client does not write to the database (it's a daily updated lookup data table) and frequently restarts itself or reopens the database. It is not a problem when the client still uses old data until he reopens the database as long as the database is still usable. From what I understand this should work. Do you agree? –  Udo G  Apr 8 '11 at 12:50
1 
@Udo G.: If your client does no updates there should be no danger of journal mix-ups. I tried deleting a DB file and my reader process continued with no issues. SQLite appears to use a single file descriptor - I am not sure that it always does so. That said, we are talking about implementation/OS-specific behaviour that is not by any means documented and that may change in the future. Do you really want to base your code on a pile of assumptions like this? –  thkala  Apr 8 '11 at 14:16 
 
interestingly, this is wrong. if you delete the file while accessing it using the sqlite3 client, you get an i/o error. 

本项目采用C++编程语言结合ROS框架构建了完整的双机械臂控制系统,实现了Gazebo仿真环境下的协同运动模拟,并完成了两台实体UR10工业机器人的联动控制。该毕业设计在答辩环节获得98分的优异成绩,所有程序代码均通过系统性调试验证,保证可直接部署运行。 系统架构包含三个核心模块:基于ROS通信架构的双臂协调控制器、Gazebo物理引擎下的动力学仿真环境、以及真实UR10机器人的硬件接口层。在仿真验证阶段,开发了双臂碰撞检测算法和轨迹规划模块,通过ROS控制包实现了末端执行器的同步轨迹跟踪。硬件集成方面,建立了基于TCP/IP协议的实时通信链路,解决了双机数据同步和运动指令分发等关键技术问题。 本资源适用于自动化、机械电子、人工智能等专业方向的课程实践,可作为高年级课程设计、毕业课题的重要参考案例。系统采用模块化设计理念,控制核心与硬件接口分离架构便于功能扩展,具备工程实践能力的学习者可在现有框架基础上进行二次开发,例如集成视觉感知模块或优化运动规划算法。 项目文档详细记录了环境配置流程、参数调试方法和实验验证数据,特别说明了双机协同作业时的时序同步解决方案。所有功能模块均提供完整的API接口说明,便于使用者快速理解系统架构并进行定制化修改。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值