# insert_protein.py

def main():
    insertProtein(proteinList)

def insertProtein(proteinList):
    db = MySQLdb.connect("localhost","root","123","ijah" )
    cursor = db.cursor()

    for i,p in enumerate(proteinList):
        proId = str(i+1)
        proId = proId.zfill(8)
        proId = 'PRO'+proId
        print 'inserting ', proId, 'of', str(len(proteinList))

        proName, proUniprotId, proUniprotAbbrv = p
        pro = [proId, proName, proUniprotId, proUniprotAbbrv]
        pro = ['"'+i+'"' for i in pro]

        # Prepare SQL query to INSERT a record into the database.
        qf = 'INSERT INTO protein (pro_id,pro_name,pro_uniprot_id,pro_uniprot_abbrv) VALUES ('
        qm = pro[0]+','+pro[1]+','+pro[2]+','+pro[3]
        qr = ')'
        sql = qf+qm+qr
        # print sql

        try:
            cursor.execute(sql)
            db.commit()
        except:
            assert False, 'dbErr'
            db.rollback()

    db.close()

if __name__ == '__main__':
    main()

@@ -17,167 +17,774 @@
 .py%0A
-%0Adef main():%0A    insertProtein(proteinList)%0A%0Adef insertProtein(proteinList):%0A    db = MySQLdb.connect(%22localhost%22,%22root%22,%22123%22,%22ijah%22 )%0A    cursor = db.cursor(
+import os%0Aimport sys%0Aimport time%0Aimport json%0Aimport yaml%0Aimport MySQLdb%0Aimport pickle%0Aimport psycopg2%0Aimport postgresql_util as pg%0Afrom collections import defaultdict%0Afrom bs4 import BeautifulSoup%0Afrom urllib2 import urlopen%0Afrom datetime import datetime%0A%0Adef main(argv):%0A    assert len(argv)%3E=7%0A%0A    db = argv%5B1%5D%0A    user = argv%5B2%5D; passwd = argv%5B3%5D%0A    host = argv%5B4%5D; port = argv%5B5%5D%0A    paths = argv%5B6:%5D%0A%0A    conn = psycopg2.connect(database=db, user=user, password=passwd,%0A                            host=host, port=port)%0A    csr = conn.cursor()%0A%0A    insertProteinUniprot(csr,paths%5B0%5D)%0A%0A    conn.commit()%0A    conn.close()%0A%0Adef insertProteinUniprot(csr,fpath):%0A    proteinList = None%0A    with open(fpath, 'rb') as handle:%0A        proteinList = pickle.load(handle
 )%0A%0A 
@@ -1020,16 +1020,60 @@
 brv = p%0A
+        proName = proName.replace(%22'%22,%22''%22)%0A
         
@@ -1145,17 +1145,17 @@
  = %5B
-'
 %22'
+%22
 +i+
-'
 %22'
+%22
  for
@@ -1344,47 +1344,21 @@
 m = 
-pro%5B0%5D+','+pro%5B1%5D+','+pro%5B2%5D+','+pro%5B3%5D
+','.join(pro)
 %0A   
@@ -1406,217 +1406,158 @@
     
-# print sql%0A%0A        try:%0A            cursor.execute(sql)%0A            db.commit()%0A        except:%0A            assert False, 'dbErr'%0A            db.rollback()%0A%0A    db.close()%0A%0Aif __name__ == '__main__':%0A    main(
+csr.execute(sql)%0A%0Aif __name__ == '__main__':%0A    start_time = time.time()%0A    main(sys.argv)%0A    print(%22--- %25s seconds ---%22 %25 (time.time() - start_time)
 )%0A

migrate to postgresql