python TypeError: dict is not a sequence

报错1: TypeError: dict is not a sequence
报错2:unsupported format character ‘Y’ (0x59) at index 79

最近在写SQL语句中发现了几个常见的错误;

第一个错误

from sqlalchemy import create_engine
sql  = """
	select name from user where name like '%test%'
"""
然后执行这个SQL 就会报错

因为三个引号与单个%冲突,需要改为两个%
解决:

	select name from user where name like '%%test%%'

第二个错误

from sqlalchemy import create_engine

sql  = """
	select name from user where date_format(time,'%Y-%m-%d') = '2021-12-18'
"""
通过sqlalchemy去执行这个SQL语句
engine.execute(sql)

会报错误 unsupported format character ‘Y’ (0x59) at index 79
因为三个引号与% 会冲突,所以需要改成两个%,改成下面这个

sql  = """
	select name from user where date_format(time,'%%Y-%%m-%%d') = '2021-12-18'
"""
再去执行这个sql语句

第三个错误

date = '2021-12-18'
sql  = """
	select name from user where date_format(time,'%%Y-%%m-%%d') = '%s'
""" % date
engine.execute(sql) 执行这个SQL语句也会报错

也会报错:unsupported format character ‘Y’ (0x59) at index 79
需要换一种拼接方式

date = '2021-12-18'
sql  = """
	select name from user where date_format(time,'%%Y-%%m-%%d') = '{date1}'
""".format(date1=date)
再去执行这个SQL就没有问题了;

你可能感兴趣的:(大数据,python,sql,数据库)