久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python pymysql 執(zhí)行比較時間的sql語句,在mysql中可以順了執(zhí)行,但是在python中執(zhí)行為何報錯?

瀏覽:138日期:2022-06-10 15:32:07

問題描述

python版本:3.5 mysql 版本:5.6python pymysql 執(zhí)行比較時間的sql語句,在mysql中可以順了執(zhí)行,但是在python中執(zhí)行報錯;數(shù)據(jù)庫表為考勤打機中導出的考勤數(shù)據(jù),根據(jù)工號篩選出遲到及早退人員;在數(shù)據(jù)庫中篩選出時間在7:30:00之后以及在17:30:00之前打卡的;

數(shù)據(jù)庫表結構:

python pymysql 執(zhí)行比較時間的sql語句,在mysql中可以順了執(zhí)行,但是在python中執(zhí)行為何報錯?

以下為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í)行結果,順利得到結果:

python pymysql 執(zhí)行比較時間的sql語句,在mysql中可以順了執(zhí)行,但是在python中執(zhí)行為何報錯?

python代碼:

import pymysql.cursors#連接數(shù)據(jù)庫connect = pymysql.connect(host=’127.0.0.1’,port=3306, user=’root’, passwd=’111111’, db=’test’,charset=’utf8’,)#獲取游標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)

報錯信息:

C:UsersgsdAppDataLocalProgramsPythonPython35python.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 literalProcess finished with exit code 1

問題解答

回答1:

多行語句試試用

''' ...... ...... '''

進行包裹

回答2:

看了樓上的評論,參見這個:https://stackoverflow.com/que...

Python里使用MySQL語句,不需要添加%來轉義%,Python的MySQL模塊會默認添加轉義字符。

標簽: Python 編程
主站蜘蛛池模板: 江油市| 博湖县| 唐河县| 利津县| 桃江县| 萨嘎县| 吕梁市| 射阳县| 西贡区| 邮箱| 阳春市| 长乐市| 城市| 昭觉县| 漠河县| 虞城县| 独山县| 和龙市| 聂荣县| 墨江| 黑龙江省| 上高县| 大新县| 苗栗市| 瑞安市| 新余市| 宁都县| 青州市| 齐河县| 班玛县| 三穗县| 奉节县| 道孚县| 绥宁县| 开封县| 台中县| 梁山县| 莫力| 永定县| 图片| 嵊泗县|