【问题标题】:python: cannot concatenate 'str' and 'tuple' objects [closed]python:无法连接'str'和'tuple'对象[关闭]
【发布时间】:2015-09-11 07:01:27
【问题描述】:

我想从员工表中获取池号并将池号用作查询 1 的参数。我得到错误 python: cannot concatenate 'str' and 'tuple' objects。

import MySQLdb
import sys

factor_mm=sys.argv[1]
factor_yy=sys.argv[2]

con_pd1 = MySQLdb.connect('localhost', 'abc', '12345', 'test')

cursor_pd1=con_pd1.cursor()

query="select pool_number from employee where current_mm="+factor_mm+" and current_yy="+factor_yy+" ;"
cursor_pd1.execute(query)
pools=cursor_pd1.fetchall()
pool_list=[]
for pool in pools:
 pool_list.append(pool[0])

for pool in pool_list:
 pool_number=pool
 try:
    query1="select * from fnma_mbs where pool_number='"+pool_number+"' and current_mm="+factor_mm+" and current_yy="+factor_yy+" ;"
    cursor_pd1.execute(query1)
    cursor_seqdb1.execute(query1)

    numrows_pd1=cursor_pd1.rowcount
    numrows_seqdb1=cursor_seqdb1.rowcount

【问题讨论】:

  • 在修改你的代码时,你解决了你的问题吗?还是你仍然得到你的错误?
  • 不,实际上这是一个错误..仍然代码给出相同的错误
  • 如果您不知道“SQL 注入”是什么或意味着什么,您现在应该停下来阅读一下。

标签: python mysql


【解决方案1】:

执行查询后变量pools 包含一个元组列表(每个元组由一个元素组成),例如[('1'), ('2'), ('3')]

for pool in pools:
 pool_list.append(pools[0])

执行此代码后pool_list将包含[('1'), ('1'), ('1')],因为pools[0] == ('1')
您应该将此代码替换为

for pool in pools:
 pool_list.append(pool[0])

【讨论】:

  • 之后我在最后关闭一个关闭的连接时出错
  • @Vivek Singh 在您的问题中添加完整的错误文本。或发表评论
  • _mysql_exceptions.programmingerror : 关闭已关闭的连接
猜你喜欢
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多