Last active
August 29, 2015 14:08
-
-
Save alexpearce/d498ec926c3e76bbab0d to your computer and use it in GitHub Desktop.
Creating and manipulating a run number database.
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 | |
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