如何使用Python连接到受保护的Sqlite3数据库 [英] How to connect to a protected Sqlite3 database with Python
问题描述
由于应用程序DB browser for SQLite
,我创建了SQLite3
数据库并使用密码(测试")对其进行了保护.
为了通过Python
连接到我的数据库,我需要提供密码,但是我不知道该怎么做.我尝试了以下代码:
I created a SQLite3
database and protected it with a password ("test") thanks to the application DB browser for SQLite
.
In order to connect to my database via Python
, I need to provide the password but I can't figure out how to do that. I tried the following code:
conn=sqlite3.connect("mydatabase.db", Password="test")
cur=conn.cursor()
我的SQLite3
数据库已使用SQLCipher
加密(参见图片).
如果我运行以下代码:
My SQLite3
database has been encrypted with SQLCipher
(see image).
If I run the following code:
conn=sqlite3.connect("mydatabase.db")
cur=conn.cursor()
我收到此错误:
sqlite3.DatabaseError: file is encrypted or is not a database
如何通过密码传递密码以便通过Python与我的db
连接?
How can I pass the password in order to connect with my db
via Python?
编辑2
这里是我要实现的目标的简短摘要.我正在使用Python 3
开发一个需要预填充数据库的应用程序,但是该数据库需要使用密码进行保护.
经过广泛研究,通过Python 3
连接加密的SQLite3
数据库似乎很复杂.存在名为pysqlcipher
的库,但仅适用于Python 2.7.我的下一个问题可能是范围太广,我事先表示歉意.是否存在另一个可移植的数据库,该数据库可让我用密码保护它并仍然可以访问Python?
为了解决我的问题,我想到的另一个想法是使用zipfile
库.此链接提到zipfile
模块不支持加密,但是不清楚加密是否引用到SQLite3
数据库或zip文件中.这个想法是将不受保护的DB
压缩为受保护的zip文件,因为我似乎可以做到这一点(
Here a brief summary of what I try to achieve. I am developing an application with Python 3
requiring a pre-populated database but this database needs to be protected with a password.
After extensive research, it seems complicated to connect an encrypted SQLite3
database via Python 3
. A library calls pysqlcipher
exists but only for Python 2.7. My next question will be maybe too broadly and I apology in advance. Is there another portable database that exists allowing me to protect it with a password and still get access to Python?
Another idea that I have in mind in order to troubleshoot my problem is to use the zipfile
library. This link mentions that the zipfile
module does not support encryption but it’s not clear if encryption refers to the SQLite3
database or to the zip file. The idea would be to zip my unprotected DB
into a protected zip file as it seems I can do that (link).
The goal of this edit is to get new ideas on how to solve my problem. Thanks
推荐答案
您需要 SQLCipher 模块读取该数据库.默认的SQLite3模块不支持该功能.参见 https://github.com/sqlitebrowser/sqlitebrowser/wiki/Encrypted-Databases
You need the SQLCipher module to read that database. The default SQLite3 module has no support for that. See https://github.com/sqlitebrowser/sqlitebrowser/wiki/Encrypted-Databases
这篇关于如何使用Python连接到受保护的Sqlite3数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!