Python用mysql.connector select报错如何解决,,python中使用mys
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, )
编橙之家文章,
相关内容
- Python什么方法可以将多行unicode字符串前u去除,pythonu
- 正则匹配出每一条记录后面的python字符串,怎么做?,
- Ruby语言对于研究网络安全方面会有提升吗?,ruby网络安
- Django写博客model关于这块该怎么写,djangomodel,初学编程
- Python第三方模块中有限制频率的模块吗,python第三方
- Python将字典转成scipy sparse matrix求方法,scipysparse,我的数
- 求指导python spark读入文件不能正常载入的原因,python
- Python爬虫爬豆瓣遇到Error 403问题怎么解决,python403,昨天
- Python Java间导致json格式错误怎么解决,pythonjson,python端
- python操作mysql数据库,数据引擎设置为myisam时能够插入
评论关闭