about summary refs log tree commit diff
path: root/scripts/maintenance/Update_Case_Attributes_MySQL_tab.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2022-03-10 08:55:26 +0300
committerFrederick Muriuki Muriithi2022-03-10 08:55:26 +0300
commit4a7e2c1602ed82aabd7d04953067ba49cb1cebff (patch)
treeea8c0cf11f1629d79844b2a4e9bb49a69e0bba33 /scripts/maintenance/Update_Case_Attributes_MySQL_tab.py
parent80b02d37f60d172be01bf8cd62bd84b406b1e0dd (diff)
downloadgenenetwork2-4a7e2c1602ed82aabd7d04953067ba49cb1cebff.tar.gz
Use context manager with database connection
Use the `with` context manager with database connections and cursors
to ensure that they are closed once they are no longer needed.

Where it was not feasible to use the `with` context manager without a
huge refactor/rewrite, the cursors and connections are closed manually.
Diffstat (limited to 'scripts/maintenance/Update_Case_Attributes_MySQL_tab.py')
-rw-r--r--scripts/maintenance/Update_Case_Attributes_MySQL_tab.py23
1 files changed, 9 insertions, 14 deletions
diff --git a/scripts/maintenance/Update_Case_Attributes_MySQL_tab.py b/scripts/maintenance/Update_Case_Attributes_MySQL_tab.py
index bf796df4..a3cd1c35 100644
--- a/scripts/maintenance/Update_Case_Attributes_MySQL_tab.py
+++ b/scripts/maintenance/Update_Case_Attributes_MySQL_tab.py
@@ -10,18 +10,13 @@ import time
 import csv
 ########################################################################
 
-mydb = MySQLdb.connect(host='localhost',
-    user='username',
-    passwd='',
-    db='db_webqtl')
-cursor = mydb.cursor()
+with MySQLdb.connect(
+        host='localhost', user='username', passwd='', db='db_webqtl') as mydb:
+    with mydb.cursor() as cursor:
 
-csv_data = csv.reader(file('GN711_pvalues.txt'), delimiter ="\t")
-for row in csv_data:
-
-    cursor.execute("""UPDATE ProbeSetXRef SET pValue = %s WHERE ProbeSetFreezeId = %s AND ProbeSetId = %s """,
-          (row))
-#close the connection to the database.
-mydb.commit()
-cursor.close()
-print("Done")
\ No newline at end of file
+        csv_data = csv.reader(file('GN711_pvalues.txt'), delimiter ="\t")
+        for row in csv_data:
+            cursor.execute(
+                """UPDATE ProbeSetXRef SET pValue = %s WHERE ProbeSetFreezeId = %s AND ProbeSetId = %s """,
+                (row))
+print("Done")