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,但是上面的为什么就可以。。求高手解答

阅读 8k
2 个回答
✓ 已被采纳新手上路,请多包涵

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

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

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

"""select * from person where name = %s """ 
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题