Python无法修改MySQL,但用户可以

时间:2015-12-14 22:59:32

标签: python mysql python-2.7

很抱歉,如果这个问题很愚蠢,我将在2天内学习python

我一直在试图理解为什么我的python脚本可以运行SELECT语句但不能运行UPDATEDELETE语句的墙上。

我认为这是MySQL问题,而不是Python问题,但我无法再进行问题排查

pcheck.py

import re
import time
import json
import MySQLdb
import requests
from array import *

conn = MySQLdb.connect([redacted])
cur = conn.cursor()

sql1 = "SELECT pkey,pmeta FROM table1 WHERE proced = 0 LIMIT 1"
cur.execute(sql1)
row = cur.fetchone()
while row is not None:
    print "row is: ",row[0]
    rchk = [
     r"(SHA256|MD5)",
     r"(abc|def)"
     ]
    for trigger in rchk:
        regexp = re.compile(trigger)
        pval = row[1]
        if regexp.search(pval) is not None:
            print "matched on: ",row[0]
            sql2 = """INSERT INTO table2 (drule,dval,dmeta) VALUES('%s', '%s', '%s')"""
            try:
                args2 = (trigger, pval, row[1])
                cur.execute(sql2, args2)
                print(cur._last_executed)
            except UnicodeError:
                print "pass-uni"
            break
    else:
        pass

    sql3 = """UPDATE table1 SET proced=1 WHERE pkey=%s"""
    args3 = row[0]
    cur.execute(sql3, args3)
    print(cur._last_executed)
    row = cur.fetchone()

sql3 = """DELETE FROM table1 WHERE proced=1 AND last_update < (NOW() - INTERVAL 6 MINUTE)"""
cur.execute(sql3)
print(cur._last_executed)
cur.close()
conn.close()
print "Finished"

实际(并且令人惊讶的预期)输出:

输出

scrape@:~/python$ python pcheck.py 
row is:  0GqQ0d6B
UPDATE table1 SET proced=1 WHERE pkey='0GqQ0d6B'
DELETE FROM table1 WHERE proced=1 AND last_update < (NOW() - INTERVAL 6 MINUTE)
Finished

但是,数据库没有更新。我检查了查询是否发送到MySQL:

MySQL日志

"2015-12-14 22:53:56","localhost []","110","0","Query","SELECT `pkey`,`pmeta` FROM `table1` WHERE `proced`=0 LIMIT 200"
"2015-12-14 22:53:57","localhost []","110","0","Query","UPDATE `table1` SET `proced`=1 WHERE `pkey`='0GqQ0d6B'"
"2015-12-14 22:53:57","localhost []","110","0","Query","DELETE FROM table1 WHERE proced=1 AND last_update < (NOW() - INTERVAL 6 MINUTE)"

但是,行0GqQ0d6B的proced值仍然不是1

如果我通过Sqlyog(以用户身份登录)进行相同的查询,则查询会按预期工作。

2 个答案:

答案 0 :(得分:0)

这类问题可能非常令人沮丧。你确定这里没有多余的空间吗?

print "row is:*"+row[0]+"*"

或许注释掉

for trigger in rchk:

部分,并在周围撒上一些打印声明?

答案 1 :(得分:0)

评论者Bob Dylan能够推断出更改后需要提交的游标。