Skip to content

Instantly share code, notes, and snippets.

@gregroberts
Last active August 29, 2015 14:07
Show Gist options
  • Save gregroberts/6e45162d6fa1844e514d to your computer and use it in GitHub Desktop.
Save gregroberts/6e45162d6fa1844e514d to your computer and use it in GitHub Desktop.
import sqlite3
#this one line is all you need to set up a database
connection = sqlite3.connect('example')
#a cursor object is used to execute queries against the db
cursor = connection.cursor()
#create a table
cursor.execute('CREATE TABLE example (id real, value text)')
#insert 100 values into the table
for i in range(100):
cursor.execute('INSERT INTO example VALUES (%d, \'%s\')' % (i, 'value %s' % str(i)))
#commit the records to memory
connection.commit()
0: <sqlite3.Cursor object at 0x02601C20>
#select a slice of the data
cursor.execute('SELECT * from example where id > 90').fetchall()
2: [(91.0, u'value 91'),
(92.0, u'value 92'),
(93.0, u'value 93'),
(94.0, u'value 94'),
(95.0, u'value 95'),
(96.0, u'value 96'),
(97.0, u'value 97'),
(98.0, u'value 98'),
(99.0, u'value 99')]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment