Created
May 26, 2018 23:05
-
-
Save json-m/ccfbee01eef5619d4122b1a76d9f11dc to your computer and use it in GitHub Desktop.
sqlite3 example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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