Python用mysql.connector select报错如何解决,,python中使用mys


python中使用mysql.connector连接mysql时,几乎一样的语句insert可以,但是select就报错

mysql的数据表是这样的:
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | cxx | 25 |
+----+------+------+
我的python代码:

cursor = conn.cursor()add_user = """insert into person(name,age) VALUES (%s,%s)"""data_user = (newName,newAge)cursor.execute(add_user,data_user)conn.commit()cursor.close()cursor = conn.cursor()select_user = """select * from person where name = (%s)"""select_data = (newName)cursor.execute(select_user,select_data)conn.commit()values = cursor.fetchall()values

上半部分的插入语句是可以正常执行的,但是下半部分的搜索语句总是报错,
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
应该是字符串处理问题。。select_data没有替换掉select_user中的%s,但是上面的为什么就可以。。求高手解答

"""select * from person where name = (%s)""" 

估计是这里错了,为何要加括号呢?换成下面的试试

"""select * from person where name = %s """ 

最终解决方案:
select_user = r'select * from person where name = %s'
select_data = (newName, )

编橙之家文章,

评论关闭