python SQL模糊查询语句问题

Python014

python SQL模糊查询语句问题,第1张

在Python上如果使用sql语句

select * from table_name where field_name like '%上海%'

执行的时候会出现pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax)的错误,这个是因为模糊查询使用like %%出现了问题。

解决:

字符串使用%需要转义,将sql语句改为:

select * from table_name where field_name like '%%上海%%'即可。

pythonsql存储过程传出参数有两种模式。

1、普通传递参数。输入以下代码:conn=MySQLdb.connect(user=root,passwd=123456,host=192.168.101.23,db=cmdb)。orange_id=98。sql=select×fromorangewhereid=%s%orange_id。cursor=conn.cursor(sql)。cursor.execute()。

2、使用字典dict类型传递参数。输入以下代码:sql=select×fromorangewhere%(color)s,%(weight)s。values={color:yellow,weight:30}。cursor.execute(sql,values)。

您好,Python语句中SQL不被识别是因为Python和SQL是两种不同的编程语言,它们之间没有直接的联系。Python是一种面向对象的编程语言,而SQL是一种关系型数据库编程语言。因此,Python不能直接识别SQL语句,也不能直接执行SQL语句。要在Python中使用SQL,您需要使用Python的数据库接口,如MySQLdb,psycopg2,SQLite等,以便将Python程序与SQL数据库连接起来,并执行SQL语句。