I have a CSV file that that I'm reading line by line and inserting the data into a database. Well..I'm trying to insert the data into the database, but I'm getting an error that I cannot figure out. The relevant portion of my code is listed below. Any ideas?
for line in flines:
line = line.rstrip().split(",")
if line[0] != "":
newlist = []
if len(line) > 7:
new2 = listaddr(line[1], line[2:-5])
newlist.extend([str(line[0]).rstrip(), new2.rstrip(), str(line[-5]).rstrip(), str(line[-4]).rstrip(), int(str(line[-3]).rstrip()), int(str(line[-2]).rstrip()), str(line[-1]).rstrip()])
else:
newlist.extend([str(line[0]).rstrip(), str(line[1]).rstrip(), str(line[2]).rstrip(), str(line[3]).rstrip(), int(str(line[4]).strip()), int(str(line[5]).rstrip()), str(line[6]).rstrip()])
if newlist[1] is not None:
if newlist[1].startswith('"'):
newlist[1] = newlist[1][1:].rstrip()
if newlist[1].endswith('"'):
newlist[1] = newlist[1][:-2].rstrip()
tuplist = tuple(newlist)
cur.execute("INSERT INTO bom (Part_No, Description, Model, Type, Per_Eng, Cntr_Qty, Location) VALUES(%s, %s, %s, %s, %d, %d, %s)" % tuplist)
The error I'm getting is:
2014-12-02 07:53:19,031 :_mysql_exceptions.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 \'0000, "BODY COMP,OIL JET", FR1B, AC1, 2, 300, J108)\' at line 1')
The first element of the tuple is getting cutoff. It should read "15280RZP 0000", but it looks like MySQL is only picking up the last four charters. Except for that, everything else looks the way it should. I snipped some code where I'm writing each element in the tuple to a text file, and when I check that file, all of the elements in the tuple show up correctly, so I'm not sure what it is I'm doing incorrectly here.