简体   繁体   中英

MySQL error 1064 (42000) when using mysql.connector

Currently getting an error "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 '%s)' at line 1" when I run this code:

    def initiate_sale():
        while True:
            cx_shopped_before = input('Has customer shopped with us before? Y/N: ')
            if cx_shopped_before.upper() == 'Y':
                cust_email = input('Please enter customers email address')
                with mysql.connector.connect(host=HOSTNAME, user=USER_NAME, password=PASSWORD) as mysql_connection:
                    with mysql_connection.cursor() as mysql_cursor:
                        mysql_cursor.execute('USE florist')
                        query = "SELECT CusID, cus_fname, cus_lname FROM customer WHERE cus_email = (%s)"
                        mysql_cursor.execute(query, cust_email)
                        break
            elif cx_shopped_before.upper() == 'N':
                pass
            else:
                print('Invalid choice, use Y or N')

I believe it to be a simple bracket or comma because its just a single item in a tuple in the execute statement but i can't find where. Thanks in advance

I could be wrong but I don't think you need the parentheses around %s

query = "SELECT CusID, cus_fname, cus_lname FROM customer WHERE cus_email = %s"

Try mysql_cursor.execute(query, tuple(cust_email)) instead of mysql_cursor.execute(query, cust_email) .

Use a tuple to replace the pace holder, it needs atm least 2 dimensions

 query = """SELECT CusID, cus_fname, cus_lname FROM customer WHERE cus_email = (%s)"""
 mysql_cursor.execute(query, (cust_email,))

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