0

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.

shee
  • 165
  • 1
  • 10
  • Use [SQLCipher](https://www.zetetic.net/sqlcipher/) to perform encryption and decryption with a password from your python code. – Armaggheddon Aug 26 '21 at 11:08
  • 1
    Is it answer for your question? https://stackoverflow.com/questions/50381616/how-to-connect-to-a-protected-sqlite3-database-with-python – mascai Aug 26 '21 at 11:08
  • 2
    Pointless. Your computer if you are admin (if its a corporate environment you are not), then you can set the permissions which is simple, but then you hardly need to protect. If its on someone else's computer they can just read the python password from your file. – jwal Aug 26 '21 at 11:09
  • @Armaggheddon could you exactly show me as its new for me to use this. – shee Aug 26 '21 at 11:10
  • @jwal I should use it in someone else's system so I am protecting the python file as well so the code cannot be read or accessed – shee Aug 26 '21 at 11:11
  • Right now i dont have a ready example to copy paste in an answer, I will get one when i get some free time if no one answers. Update: As said, see the answer below from [@Sabil](https://stackoverflow.com/users/16555279/sabil) :) – Armaggheddon Aug 26 '21 at 11:19

1 Answers1

2

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
Sabil
  • 3,750
  • 1
  • 5
  • 16
  • not able to use the sudo command on windows – shee Aug 26 '21 at 11:18
  • 1
    See at the end of [this page](https://www.zetetic.net/sqlcipher/introduction/). Windows binaries are not provided so you have to compile your own or **buy** a prebuilt one. Is just easier to use a linux based system. – Armaggheddon Aug 26 '21 at 11:27
  • @shee You follow attached links – Sabil Aug 26 '21 at 11:31