Python中执行SQL报错unsupported format character ‘Y‘ (0x59) at index 34

Python中执行SQL报错unsupported format character ‘Y’ (0x59) at index 34

from sqlalchemy import create_engine
engine_ts = create_engine('mysql+pymysql://root:[email protected]:3306/dbmysql?charset=utf8&use_unicode=1') 
sql = "select date_format(t.trade_date,'%Y-%m-%d %H:%i:%s') as date from tmp_date ;"
data = pd.read_sql(sql, con=engine_ts,index_col="date")

报错如下:

    131 conn = self._get_db()
    133 if args is not None:
--> 134     query = query % self._escape_args(args, conn)
    136 return query

ValueError: unsupported format character 'Y' (0x59) at index 34

问题语句:

select date_format(t.trade_date,‘%Y-%m-%d %H:%i:%s’) as date

%字符被转义为%字符串,因此%将被视为普通字符,而不是通配符

修改后

select date_format(t.trade_date,‘%%Y-%%m-%%d %%H:%%i:%%s’) as date

执行成功!!

你可能感兴趣的:(MySQL,Python,python,sql,android)