Mercurial > hg > nsaunier > traffic-intelligence
comparison python/storage.py @ 711:523eda2fafd4
added function to create index
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Wed, 22 Jul 2015 17:54:33 -0400 |
| parents | f83d125d0c55 |
| children | 21aeadcfbabb |
comparison
equal
deleted
inserted
replaced
| 710:70a3cdf0dbb3 | 711:523eda2fafd4 |
|---|---|
| 29 'deletes the table with names in tableNames' | 29 'deletes the table with names in tableNames' |
| 30 try: | 30 try: |
| 31 cursor = connection.cursor() | 31 cursor = connection.cursor() |
| 32 for tableName in tableNames: | 32 for tableName in tableNames: |
| 33 cursor.execute('DROP TABLE IF EXISTS '+tableName) | 33 cursor.execute('DROP TABLE IF EXISTS '+tableName) |
| 34 except sqlite3.OperationalError as error: | |
| 35 printDBError(error) | |
| 36 | |
| 37 def createIndex(filename, tableName, columnName, createAnyway = False): | |
| 38 '''Creates an index for the column in the table | |
| 39 I will make querying with a condition on this column faster''' | |
| 40 try: | |
| 41 connection = sqlite3.connect(filename) | |
| 42 cursor = connection.cursor() | |
| 43 if createAnyway: | |
| 44 notExists = "" | |
| 45 else: | |
| 46 notExists = "IF NOT EXISTS " | |
| 47 cursor.execute("CREATE INDEX "+notExists+tableName+"_"+columnName+"_index ON "+tableName+"("+columnName+")") | |
| 48 connection.commit() | |
| 49 connection.close() | |
| 34 except sqlite3.OperationalError as error: | 50 except sqlite3.OperationalError as error: |
| 35 printDBError(error) | 51 printDBError(error) |
| 36 | 52 |
| 37 # TODO: add test if database connection is open | 53 # TODO: add test if database connection is open |
| 38 # IO to sqlite | 54 # IO to sqlite |
