Skip to content

Instantly share code, notes, and snippets.

@json-m
Created May 26, 2018 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save json-m/ccfbee01eef5619d4122b1a76d9f11dc to your computer and use it in GitHub Desktop.
Save json-m/ccfbee01eef5619d4122b1a76d9f11dc to your computer and use it in GitHub Desktop.
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