Skip to content

Instantly share code, notes, and snippets.

@benhosmer
Last active December 18, 2015 00:48
Show Gist options
  • Save benhosmer/5698883 to your computer and use it in GitHub Desktop.
Save benhosmer/5698883 to your computer and use it in GitHub Desktop.
sqlite3 quickstart with python.
# Create a table with the following SQL syntax:
# $ sqlite3 db/testing.db
# create table places(id integer primary key, name text, xloc int, yloc int);
# insert into places values(NULL, 'Stream', 300.3, 100.4);
# select * from places;
# 1|Stream|300.3|100.4
# for NULL in SQL, use the Python None
import sqlite3
con = sqlite3.connect('db/testing.db')
def read_records():
with con:
cur = con.cursor()
cur.execute("select * from places")
rows = cur.fetchall()
for row in rows:
print row
def add_records(idnum, name, xloc, yloc):
'''add_records(idnum=None, name='My House', xloc=112.1, yloc=113.2)
'''
with con:
cur = con.cursor()
cur.execute("INSERT INTO places VALUES (?, ?, ?, ?)", (idnum, name, xloc, yloc))
con.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment