Skip to content

Instantly share code, notes, and snippets.

@informationsea
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save informationsea/9245248 to your computer and use it in GitHub Desktop.
Save informationsea/9245248 to your computer and use it in GitHub Desktop.
Create SQLite Index for all tables.
#!/usr/bin/env python
import argparse
import sqlite3
def _main():
parser = argparse.ArgumentParser(description="SQLite3 Helper (Create Index)")
parser.add_argument('db')
parser.add_argument('table', nargs='*')
options = parser.parse_args()
conn = sqlite3.connect(options.db)
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cur.execute('SELECT name FROM sqlite_master WHERE type = "table"')
for name, in cur:
if options.table and name not in options.table:
continue
cur2 = conn.cursor()
print 'SELECT * FROM {}'.format(name)
cur2.execute('SELECT * FROM {}'.format(name))
row = cur2.fetchone()
print type(row), row.keys()
for key in row.keys():
conn.execute('CREATE INDEX IF NOT EXISTS "{name}__{key}__index" ON {name}("{key}")'.format(name=name, key=key))
conn.commit()
if __name__ == '__main__':
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment