shee
shee

Reputation: 145

password protect or encrypt sqlite db file in python

I am generating a sqlitedb file with below code

import sqlite3
conn = sqlite3.connect('../sqlitedb/sample_db.db')

Is there any way I can password protect this file or some kind of security in python or generally in windows so nobody can access it.

Upvotes: 0

Views: 4983

Answers (1)

Sabil
Sabil

Reputation: 4510

This Solution Will Work on Linux OS Only

Install sqlcipher:

sudo apt-get install sqlcipher

Install sqlcipher package:

pip install pysqlcipher3

Sample Code:

from pysqlcipher3 import dbapi2 as sqlite
conn = sqlite.connect('test.db')
c = conn.cursor()
c.execute("PRAGMA key='password'")
c.execute("PRAGMA cipher_compatibility = 3")
c.execute('''create table stocks (date text, trans text, symbol text, qty real, price real)''')
c.execute("""insert into stocks values ('2006-01-05','BUY','RHAT',100,35.14)""")
conn.commit()
c.close()

For Windows You Can Follow Below Links:

  1. Install pysqlcipher3 windows
  2. Compile SQLite with SQLCipher on Windows

Upvotes: 2

Related Questions