I'm trying to populate a table in a MySQL database with the values for each row coming from a text file, using this SQL syntax:
INSERT INTO Table SET
fieldname1 = "value1",
fieldname2 = "value2",
fieldname3 = "value3";
I used a nested for
loop to print a string (named SQL3
) that uses the same field names for every line of the text file, trying to take care of the syntax in the right way. Seems like I'm getting close but it still throws a SQL syntax error at me, and I seem blind for what might be going on. There might be still more issues hidden underneath as it's the first time trying to connect to a db through Python.
Hopefully someone can help find the issue(s). Is some important info missing?
This is the code:
import MySQLdb
fieldNames = """Org_rowNr_countsheet
Orig_row_10604
pt3
pt3_ensembl_id
status
pt2_meth
pt3_meth
pt2_kegg_id
pt2_uniprotID
pt3_kegg_hit
pt3_uniprot
pt2_contig
pt2_start
pt2_stop
pt2_strand
pt3_contig
pt3_start
pt3_stop
pt3_strand
"""
fieldnames = fieldnames.strip().split("\n")
myconnection = MySQLdb.connect(host = "localhost", user = 'root', passwd = "mock", db="pt3_annot")
mycursor = myconnection.cursor()
infilename = "/home/oaklander114/winshare/mysql_pt3/pt3_annot_ids_reduced.csv"
infile = open(infilename, 'r')
linenumber = 0
for line in infile:
if linenumber > 0:
line = line.strip("\n\r")
fields = line.split(',')
print 'INSERT INTO ids SET '
for i, fieldname in enumerate(fieldnames):
query = []
if i < 18:
SQL1 = """
%s = '%s',
""" % (fieldname, fields[i])
query.append(SQL1)
else:
SQL2 = """
%s = '%s';
""" % (FieldName, Fields[i])
query.append(SQL2)
SQL3 = " ".join(query)
print SQL3
mycursor.execute(SQL3)
linenumber += 1
infile.close()
mycursor.close()
myconnection.commit()
myconnection.close()
This is the error message:
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Org_rowNr_countsheet = '10578',' at line 1")
I thought it would be useful to show the print out of the syntax at the point of the error as the script seems to be generating it (looks right to me):
INSERT INTO ids SET
Org_rowNr_countsheet = '10579',
Orig_row_10604 = '',
pt3 = '300002',
etc... But the error relates to the first line. Here is a more detailed error report:
%run /home/oaklander114/winshare/mysql_pt3/insert_rows_ids.py
INSERT INTO ids SET
Org_rowNr_countsheet='10578',
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
/home/oaklander114/Canopy/appdata/canopy-1.4.1.1975.rh5-x86_64/lib/python2.7/site- packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
202 else:
203 filename = fname
--> 204 __builtin__.execfile(filename, *where)
/home/oaklander114/winshare/mysql_pt3/insert_rows_ids.py in <module>()
52 SQL3 = " ".join(query)
53 print SQL3
---> 54 MyCursor.execute(SQL3)
55
56 LineNumber += 1
/home/oaklander114/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/MySQL_python-1.2.5- py2.7-linux-x86_64.egg/MySQLdb/cursors.pyc in execute(self, query, args)
203 del tb
204 self.messages.append((exc, value))
--> 205 self.errorhandler(self, exc, value)
206 self._executed = query
207 if not self._defer_warnings: self._warning_check()
/home/oaklander114/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/MySQL_python-1.2.5-py2.7-linux-x86_64.egg/MySQLdb/connections.pyc in defaulterrorhandler(***failed resolving arguments***)
34 del cursor
35 del connection
---> 36 raise errorclass, errorvalue
37
38 re_numeric_part = re.compile(r"^(\d+)")
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Org_rowNr_countsheet='10578',' at line 1")
These are the columns of the ids
table:
mysql> SHOW COLUMNS FROM ids;
+----------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+-------+
| Org_rowNr_countsheet | varchar(255) | NO | PRI | NULL | |
| Org_row_10604 | varchar(255) | YES | | NULL | |
| pt3 | varchar(255) | YES | | NULL | |
| pt3_ensembl_id | varchar(255) | YES | | NULL | |
| status | varchar(255) | YES | | NULL | |
| pt2_meth | varchar(255) | YES | | NULL | |
| pt3_meth | varchar(255) | YES | | NULL | |
| pt2_kegg_id | varchar(255) | YES | | NULL | |
| pt2_uniprotID | varchar(255) | YES | | NULL | |
| pt3_kegg_hit | varchar(255) | YES | | NULL | |
| pt3_uniprot | varchar(255) | YES | | NULL | |
| pt2_contig | varchar(255) | YES | | NULL | |
| pt2_start | varchar(255) | YES | | NULL | |
| pt2_stop | varchar(255) | YES | | NULL | |
| pt2_strand | varchar(255) | YES | | NULL | |
| pt3_contig | varchar(255) | YES | | NULL | |
| pt3_start | varchar(255) | YES | | NULL | |
| pt3_stop | varchar(255) | YES | | NULL | |
| pt3_strand | varchar(255) | YES | | NULL | |
+----------------------+--------------+------+-----+---------+-------+
19 rows in set (0.00 sec)
MyCursor.execute(SQL3) <\code>
– oaklander114 Oct 05 '14 at 06:25