[英]Python, MySQL not inserting when using variable. Can use exact value of variable in place, and it works
具有以下代碼:
import os
import sys
import time
import MySQLdb
if __name__=="__main__":
dbcon = MySQLdb.connect(host="host", port=3306, user="db", passwd="passwd", db="adki")
dbcur = dbcon.cursor()
deliveryCount = 0
bounceBadMailbox = 0
bounceInactiveAccount = 0
bouncePolicyRelated = 0
bounceSpamRelated = 0
bounceQuotaIssues =0
while True:
#type, timeLogged,timeQueued,orig,rcpt,orcpt,dsnAction,dsnStatus,dsnDiag,dsnMta,bounceCat,srcType,srcMta,dlvType,dlvSourceIp,dlvDestinationIp,dlvEsmtpAvailable,dlvSize,vmta,jobId,envId,queue,vmtaPool
line = sys.stdin.readline()
logList = line.split(',')
bounceType = str(logList[0])
if bounceType != "type":
bounceType = str(logList[0])
bounceCategory = logList[10]
emailAddress = logList[4]
jobId = str(logList[23])
fwrite = open("debug.log","a")
fwrite.write(jobId)
if bounceType == "d":
deliveryCount += 1
fwrite = open("debug2.log","a")
fwrite.write(str(jobId))
dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + 1 WHERE id = ' + jobId)
dbcon.commit()
fwrite = open("debug2.log","w")
fwrite.write("out of true loop")
dbcon.close()
python新手。 這真讓我難過。 上面的SQL語句不起作用(pmta_delivered的值仍然為0),但是我可以運行:
dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + 1 WHERE id = ' + '42')
它有效!?!。 在“ debug2.log”中,我寫了“ 42”值。 這是怎么回事??
數據庫模式:
--
-- Table structure for table `campaign_stat_delivered`
--
CREATE TABLE IF NOT EXISTS `campaign_stat_delivered` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`pmta_delivered` int(9) NOT NULL DEFAULT '0',
`async_bounces` int(9) NOT NULL DEFAULT '0',
`bad_mailbox` int(11) NOT NULL DEFAULT '0',
`inactive_account` int(11) NOT NULL DEFAULT '0',
`policy_related` int(11) NOT NULL DEFAULT '0',
`spam_related` int(11) NOT NULL DEFAULT '0',
`quota_issues` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
再次我可以將sql更改為:
dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + 1 WHERE id = 1')
而且效果很好。 真是傻眼了。
假設您的jobId是顯示整數值的字符串,我建議
添加一個斷言,確保該字符串的長度為非零。確保該字符串為整數格式。
assert len(jobId) > 0
try:
int(jobId)
except ValueError:
print "ERROR: jobId is not in format of integer"
raise
這很可能會告訴您原因是什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.