gpt4 book ai didi

Python MYSQL 元组问题。如何将参数传递给在其他类中执行查询的函数

转载 作者:行者123 更新时间:2023-11-29 03:28:15 24 4
gpt4 key购买 nike

我有这个连接类

import MySQLdb
import config

class DB:
def connect(self):
try:
self.conn = MySQLdb.connect(config.HOST,
config.USER, config.PASS, config.MYDB)
except (AttributeError, MySQLdb.OperationalError), e:
raise e

def query(self, sql, params = ()):
try:
cursor = self.conn.cursor()
cursor.execute(sql, params)
except (AttributeError, MySQLdb.OperationalError) as e:
print 'exception generated during sql connection: ', e
self.connect()
cursor = self.conn.cursor()
cursor.execute(sql, params)
return cursor

def close(self):
try:
if self.conn:
self.conn.close()
print '...Closed Database Connection: ' + str(self.conn)
else:
print '...No Database Connection to Close.'
except (AttributeError, MySQLdb.OperationalError) as e:
raise e

这是我试图传递查询的类

import mySql_connection



class Partner:

def __init__(self,pid):
db=mySql_connection.DB()
db.connect()
Partner_data="select a.pid,a.login,a.name,a.company,a.comments,b.address,b.city,b.state,b.zip,c.email,c.phone,d.account_balance,e.account_status,f.dedupe_period,f.overall_dedupe,g.day_limit,g.month_limit from Partner a inner join Location b on a.pid=b.pid inner join Contact c on a.pid=c.pid inner join account_balance d on a.pid=d.pid inner join account_status e on a.pid=e.pid inner join dedupe f on a.pid=f.pid inner join partner_limit g on a.pid=g.pid where a.pid='%s'",pid
db.query(Partner_data)

row= db.query(Partner_data).fetchall()
logi =row[0][1]

当我运行它时,我收到错误提示:
TypeError: %: 'tuple' 和 'tuple' 不支持的操作数类型

当我尝试这个时,

import mySql_connection



class Partner:

def __init__(self,pid):
db=mySql_connection.DB()
db.connect()
Partner_data="select a.pid,a.login,a.name,a.company,a.comments,b.address,b.city,b.state,b.zip,c.email,c.phone,d.account_balance,e.account_status,f.dedupe_period,f.overall_dedupe,g.day_limit,g.month_limit from Partner a inner join Location b on a.pid=b.pid inner join Contact c on a.pid=c.pid inner join account_balance d on a.pid=d.pid inner join account_status e on a.pid=e.pid inner join dedupe f on a.pid=f.pid inner join partner_limit g on a.pid=g.pid where a.pid='%s'"
db.query(Partner_data,pid)
row= db.query(Partner_data).fetchall()
logi =row[0][1]

我遇到了这个错误,

_mysql_exceptions.ProgrammingError: (1064, "您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的 '1319''' 附近使用的正确语法")

1319 是我传递的 pid,与 *Partner_data 有同样的错误

它只适用于 '"+pid+'"但我不想那样做,有人可以帮我弄清楚我哪里做错了吗。

提前致谢。

这就是我传递pid的方式

Partner1= Partner("1319")
print Partner1.login
print Partner1.company

最佳答案

这只是一个远景,但你从哪里得到“pid”?通常“一个元组”意味着,它的

pid=('1319')

例如。

你能打印那个值吗?这也可能是一个想法,使用

pid = str(pid)

首先。那么它肯定只是一个字符串。 (也许不是想要的,而是一个字符串。)

问候!

编辑:抱歉理解晚了。您可能想尝试:

Partner_data="select a.pid,a.login,a.name,a.company,a.comments,b.address,b.city,b.state,b.zip,c.email,c.phone,d.account_balance,e.account_status,f.dedupe_period,f.overall_dedupe,g.day_limit,g.month_limit from Partner a inner join Location b on a.pid=b.pid inner join Contact c on a.pid=c.pid inner join account_balance d  on a.pid=d.pid inner join account_status e on a.pid=e.pid inner join dedupe f on a.pid=f.pid inner join partner_limit g on a.pid=g.pid where a.pid='%s'"%pid

关于Python MYSQL 元组问题。如何将参数传递给在其他类中执行查询的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33530153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com