python版本:3.5 mysql 版本:5.6
python pymysql 執(zhí)行比較時(shí)間的sql語句,在mysql中可以順了執(zhí)行,但是在python中執(zhí)行報(bào)錯(cuò);
數(shù)據(jù)庫表為考勤打機(jī)中導(dǎo)出的考勤數(shù)據(jù),根據(jù)工號(hào)篩選出遲到及早退人員;
在數(shù)據(jù)庫中篩選出時(shí)間在7:30:00之后以及在17:30:00之前打卡的;
數(shù)據(jù)庫表結(jié)構(gòu):
以下為sql語句
select * from kaoqinjilu WHERE gonghao = 6063 and date_format(datatime,'%Y-%m-%d %H:%i:%s')>DATE_ADD(date_format(datatime,'%Y-%m-%d'),INTERVAL "7:30:00" HOUR_SECOND)
and date_format(datatime,'%Y-%m-%d %H:%i:%s')<DATE_ADD(date_format(datatime,'%Y-%m-%d'),INTERVAL "17:30:00" HOUR_SECOND);
執(zhí)行結(jié)果,順利得到結(jié)果:
python代碼:
import pymysql.cursors
#連接數(shù)據(jù)庫
connect = pymysql.connect(host='127.0.0.1',port=3306, user='root', passwd='111111', db='test',charset='utf8',)
#獲取游標(biāo)
cursor = connect.cursor()
sql = "select * from kaoqinjilu WHERE gonghao = 6063 AND date_format(datatime,'%%Y-%%m-%%d %%H:%%i:%%s')>DATE_ADD(date_format(datatime,'%%Y-%%m-%%d'),INTERVAL '7:30:00' HOUR_SECOND)
and date_format(datatime,'%%Y-%%m-%%d %%H:%%i:%%s')<DATE_ADD(date_format(datatime,'%%Y-%%m-%%d'),INTERVAL '17:30:00' HOUR_SECOND)"
cursor.execute(sql)
#提交
connect.commit()
for row in cursor.fetchall():
print(row)
print('遲到早退人數(shù)',cursor.rowcount)
報(bào)錯(cuò)信息:
C:\Users\gsd\AppData\Local\Programs\Python\Python35\python.exe F:/100lainxiti/考勤查詢.py
File "F:/100lainxiti/考勤查詢.py", line 7
sql = "select * from kaoqinjilu WHERE gonghao = 6063 AND date_format(datatime,'%%Y-%%m-%%d %%H:%%i:%%s')>DATE_ADD(date_format(datatime,'%%Y-%%m-%%d'),INTERVAL '7:30:00' HOUR_SECOND)
^
SyntaxError: EOL while scanning string literal
Process finished with exit code 1
看了樓上的評(píng)論,參見這個(gè):https://stackoverflow.com/que...
Python里使用MySQL語句,不需要添加%
來轉(zhuǎn)義%
,Python的MySQL模塊會(huì)默認(rèn)添加轉(zhuǎn)義字符。