Skip to content

Instantly share code, notes, and snippets.

@bbengfort
Created March 3, 2015 01:30
Show Gist options
  • Save bbengfort/0e05117be1f865f7e89f to your computer and use it in GitHub Desktop.
Save bbengfort/0e05117be1f865f7e89f to your computer and use it in GitHub Desktop.
Create a fake database for testing queries.
import sqlite3
import random
def next_string(s):
strip_zs = s.rstrip('z')
if strip_zs:
return strip_zs[:-1] + chr(ord(strip_zs[-1]) + 1) + 'a' * (len(s) - len(strip_zs))
else:
return 'a' * (len(s) + 1)
def getint(maxval=20):
return random.choice(xrange(maxval))
def gendb(path, rows=100):
conn = sqlite3.connect(path)
cur = conn.cursor()
cur.execute("CREATE TABLE R (A TEXT, B INTEGER)")
cur.execute("CREATE TABLE S (A TEXT, C INTEGER)")
idx = "aa"
for jdx in xrange(rows):
cur.execute("INSERT INTO R VALUES (?, ?)", (idx, getint()))
cur.execute("INSERT INTO S VALUES (?, ?)", (idx, getint()))
idx = next_string(idx)
conn.commit()
conn.close()
if __name__ == '__main__':
print gendb('example.db')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment