2012-04-18 139 views
15

從C/C++使用sqlite3時,我瞭解到它具有隻讀模式選項,這非常方便以避免意外的數據損壞。 Python綁定中有這樣的事情嗎?以只讀模式從python打開sqlite3數據庫

+0

可能的重複:http://stackoverflow.com/questions/4239606/sqlite3-read-only-on-a-file-system-that-doesnt-support-locking – Chris 2012-04-18 08:53:38

+1

@Chris謝謝,我會編寫一個答案只是爲了將相同問題的一些工作留給未來的人。 – dsign 2012-04-18 09:04:41

+0

[強制python放棄原生sqlite3和使用(安裝)最新的sqlite3版本]的可能重複(http://stackoverflow.com/questions/1545479/force-python-to-forego-native-sqlite3-and-use- the-installed-latest-sqlite3-ver) – 2012-04-18 09:48:23

回答

2

由@Chris給出的鏈接,沒有。但是還有另一個用於sqlite3的包裝,它不符合PEP 249標準,並且更緊密地包裝了sqlite3,同化引擎的新功能:http://code.google.com/p/apsw/。該包裝器支持以只讀模式打開數據庫以及其他細節。

21

對於Python 3.4.0,你可以用下面的打開只讀模式數據庫:

db = sqlite3.connect('file:/path/to/database?mode=ro', uri=True) 

另見the documentation

+0

注意:這不適用於Python,只適用於Python 3 – lowtech 2018-01-31 14:10:32

7

解決方法爲Python 2.x的:

fd = os.open(filename, os.O_RDONLY) 
c = sqlite3.connect('/dev/fd/%d' % fd) 
os.close(fd) 

不是POSIX,但可用於Linux,OS/X和最現代化的Unix。

相關問題