Skip to content

Instantly share code, notes, and snippets.

@alexpearce
Last active August 29, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexpearce/d498ec926c3e76bbab0d to your computer and use it in GitHub Desktop.
Save alexpearce/d498ec926c3e76bbab0d to your computer and use it in GitHub Desktop.
Creating and manipulating a run number database.
import sqlite3
import random
# Create the database
con = sqlite3.connect('/path/to/database.db')
con.execute('CREATE TABLE runs (run INTEGER PRIMARY KEY)')
# Insert many runs...
runs = range(int(1e3))
random.shuffle(runs)
con.executemany('INSERT INTO runs (run) VALUES (?)', [(r,) for r in runs])
# ... or a single one
con.execute('INSERT INTO runs (run) VALUES (?)', (123987,))
# Remove many runs...
con.executemany('DELETE FROM runs WHERE run=?', [(r,) for r in range(100, 200)])
# ... or a single one
con.execute('DELETE FROM runs WHERE run=?', (123987,))
# Count the number of runs
nruns, = con.execute('SELECT Count(*) FROM runs').fetchone()
# Get the list of sorted runs
sorted_runs = con.execute('SELECT run FROM runs ORDER BY run ASC').fetchall()
print nruns
print sorted_runs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment