from cryptography.fernet import Fernet import sqlite3 # Generate key to use it #print(Fernet.generate_key()) key = b'rMqQ4gNSsvkubQnn9CmW25PTFDwNlQPUp7YN4qDVSts=' cipher_suite = Fernet(key) db_name = 'mydb.db' conn = sqlite3.connect(db_name) cursor = conn.cursor() # create a table cursor.execute('create table if not exists items (name STRING, price INTEGER)') def add_item(name, price): name = cipher_suite.encrypt(name.encode()) price = cipher_suite.encrypt(str(price).encode()) cursor.execute('insert into items (name, price) values (?, ?)', (name, price)) conn.commit() add_item('test', 50) def get_items(): results = cursor.execute('select * from items').fetchall() for item in results: name = cipher_suite.decrypt(item[0]).decode() price = cipher_suite.decrypt(item[1]).decode() print(name, price) get_items()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter