最新消息: 新版网站上线了!!!

python 插入mysql信息提示错误unsupported format character 'Y' (0x59) at index 356


tDate = time.strftime("%Y-%m-%d",time.localtime(time.time()))

sql = " insert into content(catid,typeid,title,adddate) values ('%d','%d','%s','%Y-%m-%d') " % (23,0,titleContents,tDate)

修改为

sql = " insert into content(catid,typeid,title,adddate) values ('%d','%d','%s','%%Y-%%m-%%d') " % (23,0,titleContents,tDate)

产生原因:因为python执行的sql中存在类似DATE_FORMAT(CREATE_TIME, ‘%Y-%m-%d’) 的写法,其中%Y与python的参数%s冲突


解决方法如下:

使用:str_to_date(%s,'%%Y-%%m-%%d')

sql = " insert into 3qphp_content(catid,typeid,title,adddate) values ('%d','%d','%s',str_to_date(%s,'%%Y-%%m-%%d')) " % (23,0,titleContents,tDate)


转载请注明:谷谷点程序 » python 插入mysql信息提示错误unsupported format character 'Y' (0x59) at index 356