Search code examples
mysqlpython-3.xload-data-infile

Python3 cursor.execute correct syntax


Trying to insert some data using python3 and a local csv file - what is wrong with this syntax? python keeps saying

mysql.connector.errors.ProgrammingError: 1064 (42000): 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 ' FIELDS TERMINATED BY ','' at line 1

cursor.execute("""LOAD DATA LOCAL INFILE '/home/user/mongo_exported_users.csv' INTO TABLE users IGNORE 1 LINES, FIELDS TERMINATED BY ','""")

Solution

  • According to the documentation, there can be no comma before the FIELDS TERMINATED BY clause, and also, the IGNORE # LINES clause must come after the FIELDS TERMINATED BY clause:

    cursor.execute("""LOAD DATA LOCAL INFILE '/home/user/mongo_exported_users.csv'
        INTO TABLE users FIELDS TERMINATED BY ',' IGNORE 1 LINES""")