使用模糊查询,用WHERE语句中的LIKE语句,在使用 % 的时候要注意的点:
在Navicat中查询attribute中不含32或者为空的场景:
SELECT * FROM "map".road_link WHERE attribute NOT LIKE '%32%' or attribute IS NULL;
放到python中查询attribute中不含32或者为空的场景:
sql = """SELECT * FROM %s.%s WHERE attribute NOT LIKE '%32%' or attribute IS NULL""" % (schema, layer_name)
cur.execute(sql)
然后就会报错TypeError: not enough arguments for format string
这是因为在Python中%是一个格式化字符,所以如果需要使用%则需要写成%%。
因此:
将在Python中执行的sql语句改为:sql = """SELECT * FROM %s.%s WHERE attribute NOT LIKE '%%32%%' or attribute IS NULL""" % (schema, layer_name)
执行成功!