[http://stackoverflow.com/questions/5536362/replacing-sqlite-database-while-accessing-it/5594127#5594127]
5 |
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 Ireplacethe 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 berenamedto the final file name. In Linux,a program which has stillopenthe 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:
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 donotwant to send a bunch of sql statements from the server to the client to update the database. |
|||||
|
1
|
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:
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 threediscretesteps:
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. TheSQLite3 backup APImight be of interest to you in that case. |
|||||||||
|