Skip to content

Instantly share code, notes, and snippets.

@tzarskyz
Forked from gka/sqlite3-example.py
Created December 5, 2013 18:52
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 tzarskyz/7811062 to your computer and use it in GitHub Desktop.
Save tzarskyz/7811062 to your computer and use it in GitHub Desktop.
import sqlite3
# open connection and get a cursor
conn = sqlite3.connect(':memory:')
c = conn.cursor()
# create schema for a new table
c.execute('CREATE TABLE IF NOT EXISTS sometable (name, age INTEGER)')
conn.commit()
# insert a new row
c.execute('INSERT INTO sometable values (?, ?) ', ('John Doe', 37))
conn.commit()
# extend schema during runtime
c.execute('ALTER TABLE sometable ADD COLUMN gender TEXT')
conn.commit()
# add another row
c.execute('INSERT INTO sometable values (?, ?, ?) ', ('Jane Doe', 34, 'female'))
conn.commit()
# get a single row
c.execute('SELECT name, age FROM sometable WHERE name = ?', ('John Doe', ))
row = list(c)[0]
john = dict(name=row[0], age=row[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment