1

I have 304MB text file which contains comma(,) separated values in each line, I am reading file line by line and inserting values using for loop, but I keep getting this error after 21 rows are inserted. here is the code

import MySQLdb

myfile = open('/home/vaibhav/Desktop/question1.csv', 'r')

db = MySQLdb.connect(host="localhost", 
                 user="root", 
                  passwd="admin", 
                  db="question1agg") 

for line in myfile:

     my_line_list = line.split(',')
     string = ''
     for value in my_line_list:
            string = string + "'" + value + "',"
            query_string = string[:-1]
      final_query = "insert into question1 values"+"("+query_string+");"
      cur = db.cursor()
      cur.execute(final_query)
      db.commit()
db.close()

And this is the error i get

 Traceback (most recent call last):
 File "da.py", line 19, in <module>
   cur.execute(final_query)
   File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute
   self.errorhandler(self, exc, value)
   File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in        defaulterrorhandler
   raise errorclass, errorvalue
_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 's Solids','Men','NULL','Solid','/Products/Legwear','/Products/Legwear/AmericanEs' at line 1")
vaibhav1312
  • 863
  • 4
  • 13
  • 31

2 Answers2

0

It looks like one of your values have an apostrophe in it. You need to escape the apostrophe to avoid that error.

For example, you have a value like this:

That's right!

So your insert looks like this:

insert into question1 values('value1','That's right!','value2');

You should escape the apostrophe to make the insert like this:

insert into question1 values('value1','That''s right!','value2');
Ike Walker
  • 64,401
  • 14
  • 110
  • 109
0

Looks like on one of your lines there is a apostrophe that is messing MySQL up. From the error message -

's Solids','Men','NULL','Solid'

Your problem and it's solution is explained nicely here - Python MySQL escape special characters

Community
  • 1
  • 1
RyPeck
  • 7,830
  • 3
  • 38
  • 58