[英]Update select row in MySQL with Python
I'm writing a code that essentially queries the MySQL table first, finds a telephone number that doesn't have user assigned and updates it with new user that user specifies.我正在编写一个代码,它首先查询 MySQL 表,找到一个没有分配给用户的电话号码,并用用户指定的新用户更新它。 Here is my code:这是我的代码:
import mysql.connector
mydb = mysql.connector.connect(
host="127.0.0.1",
user="root",
password="password",
database="DID_MASTER"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT DID FROM JAX2 WHERE ASSIGNEE = ''")
myresult = mycursor.fetchall()
location = input("Enter users location: :")
name = input('Enter users name: ')
for x in myresult:
print('Free number to be used: ', x)
sql = "UPDATE JAX2 SET ASSIGNEE = %s WHERE DID = %s"
val = (name, x)
mycursor.executemany(sql, val)
mydb.commit()
break
Query works fine but problem is with updating portion.查询工作正常,但问题在于更新部分。 It returns following error:它返回以下错误:
Enter users name: Joe Smith
Free number to be used: (1235136500,)
Traceback (most recent call last):
File "/Users/user1/Desktop/old mac Desktop/DID Project/DB_Testing.py", line 27, in <module>
mycursor.executemany(sql, val)
File "/Users/user1/Library/Python/3.9/lib/python/site-packages/mysql/connector/cursor_cext.py", line 406, in executemany
self.execute(operation, params)
File "/Users/user1/Library/Python/3.9/lib/python/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/Users/user1/Library/Python/3.9/lib/python/site-packages/mysql/connector/connection_cext.py", line 738, in prepare_for_mysql
raise ProgrammingError(
mysql.connector.errors.ProgrammingError: Could not process parameters: str(Joe Smith), it must be of type list, tuple or dict
UPDATE ERROR:更新错误:
raceback (most recent call last):
File "/Users/nbecirovic/Desktop/old mac Desktop/DID Project/DB_Testing.py", line 27, in <module>
mycursor.executemany(sql, val)
File "/Users/nbecirovic/Library/Python/3.9/lib/python/site-packages/mysql/connector/cursor_cext.py", line 406, in executemany
self.execute(operation, params)
File "/Users/nbecirovic/Library/Python/3.9/lib/python/site-packages/mysql/connector/cursor_cext.py", line 266, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/Users/nbecirovic/Library/Python/3.9/lib/python/site-packages/mysql/connector/connection_cext.py", line 726, in prepare_for_mysql
result = self._cmysql.convert_to_mysql(*params)
_mysql_connector.MySQLInterfaceError: Python type tuple cannot be converted
It seems that you are passing x directly to the update query, But x is a tuple so you must get the first element and then add it to the SQL update query.似乎您将 x 直接传递给更新查询,但 x 是一个元组,因此您必须获取第一个元素,然后将其添加到 SQL 更新查询。
As @lucas-fernandes answered you have to use a list with executemany
正如@lucas-fernandes回答的那样,你必须使用带有executemany
的列表
sql = "UPDATE JAX2 SET ASSIGNEE = %s WHERE DID = %s"
did = x[0]
val = [(name, did)]
mycursor.executemany(sql, val)
mydb.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.