简体   繁体   中英

Python MySQL: Special escaped character leads to insert error

I am using parameter binding to insert into MySQL 5.7.29 via python 3.x. Somehow I am still getting an insert error while inserting '

Failed to save datasets. INSERT IGNORE INTO table (
      `name`
) 
VALUES ('L\'Test') Error  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 'Test'' at line 1

Query:

self.curr.execute(""" 
    INSERT IGNORE INTO table (
        `name`
    ) 
    VALUES (%s)
    
    """, (                                                           
        item['test'],                    
    )
)

I thought that in that case escaping is not needed?

Full query error message:

Failed to save datasets. INSERT IGNORE INTO manufacturers (name) VALUES ('L\'Oreal Deutschland GmbH Geschäftsbereich La Roche-Posay') Error  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 'Oreal Deutschland GmbH Geschäftsbereich La Roche-Posay'' at line 1

SQL mode:

SELECT @@sql_mode;

`IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

MySQL Pipeline:

class mysql_pipeline(object):
    def __init__(self):
        self.create_connection()

    def create_connection(self):
        self.conn = mysql.connector.connect(
            host        = 'rex',
            user        = 'crawler',
            passwd      = 'asdsadf3',
            database    = 'test',
            charset     = 'utf8'
        )
        self.curr = self.conn.cursor()
        self.curb = self.conn.cursor(buffered=True)

    def process_item(self, item, spider):
        self.store_db(item, spider)
        return item

    def store_db(self, item, spider):

From the syntax of your code, I assume that you are using MySQL Connector. You can write query like this.

insert_statement = (
    "INSERT IGNORE INTO tablename (columnname) "  # mind the space after (columnname) 
    "VALUES (%s)"
)
actual_data = (item['test'],) 

cursor.execute(insert_statement, actual_data)

It is easier to read and does exactly what you want to do.

reference: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

Based on the statement shown in error and the error code, your sql_mode must include NO_BACKSLASH_ESCAPES. After creating your connection, you can run:

self.curr.execute("set @@local.sql_mode=''")

That should change the sql_mode for your connection. You can choose whether to include other settings such as STRICT_ALL_TABLES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM