简体   繁体   English

您可以同时对SQLite数据库进行多次读/写吗?

[英]Can you have multiple read/writes to SQLite database simultaneously?

I am using Python with SQLite currently and wondering if it is safe to have multiple threads reading and writing to the database simultaneously. 我目前正在将Python与SQLite一起使用,并想知道让多个线程同时读取和写入数据库是否安全。 Does SQLite handle data coming in as a queue or have sort of mechanism that will stop the data from getting corrupt? SQLite是否处理作为队列进入的数据,或具有某种机制来阻止数据损坏?

This is my issue too. 这也是我的问题。 SQLite using some kind of locking mechanism which prevent you doing concurrency operation on a DB. SQLite使用某种锁定机制,防止您在数据库上进行并发操作。 But here is a trick which i use when my db are small. 但是,这是我的数据库很小时使用的一个技巧。 You can select all your tables data into memory and operate on it and then update the original table. 您可以将所有表数据选择到内存中并对其进行操作,然后更新原始表。

As i said this is just a trick and it does not always solve the problem. 就像我说的那样,这只是一个把戏,并不总是能解决问题。

I advise to create your trick. 我建议创造你的把戏。

SQLite has a number of robust locking mechanisms to ensure the data doesn't get corrupted, but the problem with that is if you have a number of threads reading and writing to it simultaneously you'll suffer pretty badly in terms of performance as they all trip over the others. SQLite具有许多健壮的锁定机制来确保数据不会被破坏,但是这样做的问题是,如果同时有多个线程对其进行读写操作,那么在性能方面,它们将遭受非常严重的影响。绊倒其他人。 It's not intended to be used this way, even if it does work. 即使它确实起作用,也不打算以这种方式使用。

You probably want to look at using a shared database server of some sort if this is your intended usage pattern. 如果这是您的预期使用模式,则可能要考虑使用某种共享数据库服务器。 They have much better support for concurrent operations. 他们对并发操作有更好的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM