Python MYSQL tupple issue.How 将参数传递给在另一个 class 中执行查询的函数

Python MYSQL tupple issue.How to pass the parameter to a function executing the query in the other class

我有这个联系class

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

这是 class 我试图传递查询的地方

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] 

当我 运行 这个时,我收到错误提示:
类型错误:% 不支持的操作数类型:'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, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1319''' at line 1")

1319 是我传递的 pid,并得到与 *Partner_data

相同的错误

它只适用于“+pid+” 但我不想那样做,有人能帮我找出我做错的地方吗?

提前致谢。

这就是我传递 pid 的方式

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

这只是一个远射,但你从哪里得到 "pid"?通常 "a tuple" 表示,其

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
  • 字符串后面没有逗号,另一个 % 是诀窍。 (Shameless stolen here (Whosebug))