首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python,使用变量时不插入MySQL。可以在适当的位置使用变量的精确值,并且它可以工作

Python,使用变量时不插入MySQL。可以在适当的位置使用变量的精确值,并且它可以工作
EN

Stack Overflow用户
提问于 2014-05-14 04:01:24
回答 1查看 63关注 0票数 0

具有以下代码:

代码语言:javascript
运行
复制
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),但我可以运行:

代码语言:javascript
运行
复制
dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + 1 WHERE id = ' + '42')

它成功了!?!。在'debug2.log‘中,我写入值'42’。怎么回事??

数据库架构:

代码语言:javascript
运行
复制
--
-- 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更改为:

代码语言:javascript
运行
复制
dbcur.execute('UPDATE campaign_stat_delivered SET pmta_delivered = pmta_delivered + 1 WHERE id = 1')

它工作得很好。真的很傻。

EN

回答 1

Stack Overflow用户

发布于 2014-05-14 05:46:31

假设您的jobId是表示整数值的字符串,我建议

添加断言,确保字符串的长度为非零确保字符串为整数格式。

代码语言:javascript
运行
复制
assert len(jobId) > 0
try:
    int(jobId)
except ValueError:
    print "ERROR: jobId is not in format of integer"
    raise

这可能会告诉你,原因是什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23640523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档