Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,MySQL语句在我输入变量的实际值时有效,但在使用变量时无效?_Python_Mysql_Stdin - Fatal编程技术网

Python,MySQL语句在我输入变量的实际值时有效,但在使用变量时无效?

Python,MySQL语句在我输入变量的实际值时有效,但在使用变量时无效?,python,mysql,stdin,Python,Mysql,Stdin,有以下代码,它是用于读取stdin和处理日志的脚本的一部分 jobId = loglist[19] deliveryCount += 1 dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %s WHERE id = %s') % (deliveryCount,jobId) dbcon.commit() dbcon.close() 我可以运行以下命令: dbcur.execute

有以下代码,它是用于读取stdin和处理日志的脚本的一部分

jobId = loglist[19]
deliveryCount += 1
dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %s WHERE id = %s') % (deliveryCount,jobId)
dbcon.commit()
dbcon.close()
我可以运行以下命令:

dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + 1 WHERE id=1')
dbcon.commit()
dbcon.close()
它会起作用的。不太确定发生了什么,而且对我来说很难快速测试,因为我无法看到脚本正在运行,因为我的程序直接输入到脚本中。我必须进行更改,重新启动馈送程序,发送电子邮件,然后检查数据库。有其他脚本,并且我能够毫无问题地在SQL语句中使用变量

对可能发生的事情有什么建议吗?还有,关于如何更快地进行测试,有什么建议吗

完整代码:

import os
import sys
import time
import MySQLdb
import csv

if __name__=="__main__":

dbcon = MySQLdb.connect(host="tattoine.mktrn.net", port=3306, user="adki", passwd="pKhL9vrMN8BsFrJ5", db="adki")
dbcur = dbcon.cursor()

    #type, timeLogged,timeQueued,orig,rcpt,orcpt,dsnAction,dsnStatus,dsnDiag,dsnMta,bounceCat,srcType,srcMta,dlvType,dlvSourceIp,dlvDestinationIp,dlvEsmtpAvailable,dlvSize,vmta,jobId,envId,queue,vmtaPool
while True:
    line = sys.stdin.readline()
    fwrite = open("debug.log","w")
   # fwrite.write(str(deliveryCount))
    fwrite.write("test2")
    dbcur.execute("INSERT INTO test(event_type) VALUES ('list')")
    dbcon.commit()

    loglist = line.split(',')



    deliveryCount = 0
    bounceType = loglist[0]
    bounceCategory = loglist[10]
    email = loglist[4]
    jobId = loglist[19]

    if bounceType == 'd':
        deliveryCount += 1


fwrite = open("debug2.log","w")
   # fwrite.write(str(deliveryCount))
fwrite.write("test3")

dbcur.execute("INSERT INTO test(event_type) VALUES (%d)", deliveryCount)
dbcon.commit()
dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %s WHERE id = %s',(deliveryCount,jobId))
dbcon.commit()
dbcon.close()

切勿使用字符串插值来运行sql查询

你应该做:

dbcur.execute(
    'UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %s WHERE id = %s',
    (deliveryCount,jobId)
)
execute函数有两个参数。带有占位符和参数元组的查询。这样,mysql将为您转义参数并防止sql注入攻击()


您的错误一定是因为您对查询结果使用了%运算符
'UPDATE campaign\u stat\u delivered SET pmta\u delivered=pmta\u delivered+%s,其中id=%s'
。对于mysql,这个查询本身(没有参数)在语法上是不正确的。必须将参数元组作为第二个参数传递给execute函数。

切勿使用字符串插值来运行sql查询

你应该做:

dbcur.execute(
    'UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %s WHERE id = %s',
    (deliveryCount,jobId)
)
execute函数有两个参数。带有占位符和参数元组的查询。这样,mysql将为您转义参数并防止sql注入攻击()


您的错误一定是因为您对查询结果使用了%运算符
'UPDATE campaign\u stat\u delivered SET pmta\u delivered=pmta\u delivered+%s,其中id=%s'
。对于mysql,这个查询本身(没有参数)在语法上是不正确的。必须将参数元组作为第二个参数传递给execute函数。

切勿使用字符串插值来运行sql查询

你应该做:

dbcur.execute(
    'UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %s WHERE id = %s',
    (deliveryCount,jobId)
)
execute函数有两个参数。带有占位符和参数元组的查询。这样,mysql将为您转义参数并防止sql注入攻击()


您的错误一定是因为您对查询结果使用了%运算符
'UPDATE campaign\u stat\u delivered SET pmta\u delivered=pmta\u delivered+%s,其中id=%s'
。对于mysql,这个查询本身(没有参数)在语法上是不正确的。必须将参数元组作为第二个参数传递给execute函数。

切勿使用字符串插值来运行sql查询

你应该做:

dbcur.execute(
    'UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + %s WHERE id = %s',
    (deliveryCount,jobId)
)
execute函数有两个参数。带有占位符和参数元组的查询。这样,mysql将为您转义参数并防止sql注入攻击()


您的错误一定是因为您对查询结果使用了%运算符
'UPDATE campaign\u stat\u delivered SET pmta\u delivered=pmta\u delivered+%s,其中id=%s'
。对于mysql,这个查询本身(没有参数)在语法上是不正确的。您必须将参数元组作为第二个参数传递给execute函数。

仍然是空的,知道吗?我想可能我永远不会逃避'while true'语句,因为我永远不会将'test3'写入调试文件。但感谢您让我知道执行sql的正确方法。@brizz只使用%s占位符(而不是%d)。另外,
execute
方法的第二个参数必须始终是元组,即使您的查询只有一个参数。像这样:dbcur.execute(“INSERT-INTO-test(event_-type)value(%s)”,(deliveryCount,))仍然是空的,知道吗?我想我可能永远不会逃避'while-true'语句,因为我从来没有将'test3'写入调试文件。但感谢您让我知道执行sql的正确方法。@brizz只使用%s占位符(而不是%d)。另外,
execute
方法的第二个参数必须始终是元组,即使您的查询只有一个参数。像这样:dbcur.execute(“INSERT-INTO-test(event_-type)value(%s)”,(deliveryCount,))仍然是空的,知道吗?我想我可能永远不会逃避'while-true'语句,因为我从来没有将'test3'写入调试文件。但感谢您让我知道执行sql的正确方法。@brizz只使用%s占位符(而不是%d)。另外,
execute
方法的第二个参数必须始终是元组,即使您的查询只有一个参数。像这样:dbcur.execute(“INSERT-INTO-test(event_-type)value(%s)”,(deliveryCount,))仍然是空的,知道吗?我想我可能永远不会逃避'while-true'语句,因为我从来没有将'test3'写入调试文件。但感谢您让我知道执行sql的正确方法。@brizz只使用%s占位符(而不是%d)。另外,
execute
方法的第二个参数必须始终是元组,即使您的查询只有一个参数。如下所示:dbcur.execute(“插入测试(事件类型)值(%s)”,(deliveryCount,)