diff 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
line wrap: on
line diff
--- a/python/storage.py	Wed Jul 22 17:01:25 2015 -0400
+++ b/python/storage.py	Wed Jul 22 17:54:33 2015 -0400
@@ -34,6 +34,22 @@
     except sqlite3.OperationalError as error:
         printDBError(error)
 
+def createIndex(filename, tableName, columnName, createAnyway = False):
+    '''Creates an index for the column in the table
+    I will make querying with a condition on this column faster'''
+    try:
+        connection = sqlite3.connect(filename)
+        cursor = connection.cursor()
+        if createAnyway:
+            notExists = ""
+        else:
+            notExists = "IF NOT EXISTS "
+        cursor.execute("CREATE INDEX "+notExists+tableName+"_"+columnName+"_index ON "+tableName+"("+columnName+")")
+        connection.commit()
+        connection.close()
+    except sqlite3.OperationalError as error:
+        printDBError(error)
+
 # TODO: add test if database connection is open
 # IO to sqlite
 def writeTrajectoriesToSqlite(objects, outputFilename, trajectoryType, objectNumbers = -1):