Last active
August 29, 2015 13:56
-
-
Save informationsea/9245248 to your computer and use it in GitHub Desktop.
Create SQLite Index for all tables.
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
#!/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