'tuple' 对象没有属性 'encode'
'tuple' object has no attribute 'encode'
代码如下(我是Python/Mysql新手):
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='user1',password='puser1',db='mm')
cursor = conn.cursor()
string1 = 'test1'
insert_query = """INSERT INTO items_basic_info (item_name) VALUES (%s)""", (string1)
cursor.execute(insert_query)
conn.commit()
当我 运行 这个代码时,我得到这个错误:
Traceback (most recent call last)
File "test3.py", line 9, in <module>
cursor.execute(insert_query)
File "C:\Users\Emanuele-PC\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\cursor.py", line 492, in execute
stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'
我看到了这个问题的不同答案,但案例与我的情况大不相同,我真的不明白我在哪里犯了错误。谁能帮帮我?
avoid SQL-injections Django documentation完全推荐使用这样的占位符:
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='user1',password='puser1',db='mm')
cursor = conn.cursor()
string1 = 'test1'
insert_query = """INSERT INTO items_basic_info (item_name) VALUES (%s)"""
cursor.execute(insert_query, (string1,))
conn.commit()
你必须在 execute 方法中传递 tuple/list 参数作为第二个参数。一切都应该没问题。
不完全是 OP 的问题,但我在向 MySQL 写入多个变量时遇到了问题。
根据 Jefferson Houp 的回答,如果添加多个字符串,您必须在 'cursor.execute' 函数中指定参数 'multi=True'。
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='user1',password='puser1',db='mm')
cursor = conn.cursor()
string1 = 'test1'
string2 = 'test2'
insert_query = """INSERT INTO items_basic_info (item_name) VALUES (%s, %s)"""
cursor.execute(insert_query, (string1, string2), multi=True)
conn.commit()
代码如下(我是Python/Mysql新手):
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='user1',password='puser1',db='mm')
cursor = conn.cursor()
string1 = 'test1'
insert_query = """INSERT INTO items_basic_info (item_name) VALUES (%s)""", (string1)
cursor.execute(insert_query)
conn.commit()
当我 运行 这个代码时,我得到这个错误:
Traceback (most recent call last)
File "test3.py", line 9, in <module>
cursor.execute(insert_query)
File "C:\Users\Emanuele-PC\AppData\Local\Programs\Python\Python36\lib\site-packages\mysql\connector\cursor.py", line 492, in execute
stmt = operation.encode(self._connection.python_charset)
AttributeError: 'tuple' object has no attribute 'encode'
我看到了这个问题的不同答案,但案例与我的情况大不相同,我真的不明白我在哪里犯了错误。谁能帮帮我?
avoid SQL-injections Django documentation完全推荐使用这样的占位符:
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='user1',password='puser1',db='mm')
cursor = conn.cursor()
string1 = 'test1'
insert_query = """INSERT INTO items_basic_info (item_name) VALUES (%s)"""
cursor.execute(insert_query, (string1,))
conn.commit()
你必须在 execute 方法中传递 tuple/list 参数作为第二个参数。一切都应该没问题。
不完全是 OP 的问题,但我在向 MySQL 写入多个变量时遇到了问题。
根据 Jefferson Houp 的回答,如果添加多个字符串,您必须在 'cursor.execute' 函数中指定参数 'multi=True'。
import mysql.connector
conn = mysql.connector.connect(host='localhost',user='user1',password='puser1',db='mm')
cursor = conn.cursor()
string1 = 'test1'
string2 = 'test2'
insert_query = """INSERT INTO items_basic_info (item_name) VALUES (%s, %s)"""
cursor.execute(insert_query, (string1, string2), multi=True)
conn.commit()