Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sqlite3 example
import sqlite3
from pathlib import Path
dbpath = Path('test.db')
def insert(uid, name):
print('inserting')
print(' ↪ uid:', uid)
print(' ↪ name:', name)
c.execute('INSERT INTO test (uid, name) values (?, ?)', (uid, name))
conn.commit()
if Path.is_file(dbpath):
print('loading db')
conn = sqlite3.connect(str(dbpath.absolute()))
c = conn.cursor()
else:
print('db does not exist, creating')
conn = sqlite3.connect(str(dbpath.absolute()))
c = conn.cursor()
print('creating table')
c.execute('CREATE TABLE test (uid integer, name text)')
conn.commit()
insert(1, 'admin')
for row in c.execute('select * from test'):
print(row)
conn.commit()
c.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.