2015-11-04 11 views
2

我有這個連接類Python的MYSQL元組issue.How的參數傳遞給函數在其他類

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] 

當我運行它,我得到錯誤說:
類型錯誤:不支持的操作數類型爲%:「元組」和「元組」

,當我試圖日是,

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服務器版本的使用權語法手冊近「1319」「」在1" 號線)

1319是我與* Partner_data

通過,並得到了同樣的錯誤的PID其唯一的工作罰款「「+ PID +」」 ,但我不想做這樣的,能有一個人請幫我找出我在哪裏做錯了。

在此先感謝。

所以這我是如何通過pid

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

回答

0

這只是一個遠射,但你從哪裏得到「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 
+0

感謝您的答覆,我只是加入我有多通過我的PID編輯.. – Shubham

+0

啊,現在我明白了。在將pid附加到Partner_data以顯示結果之前,當您使用打印(「PID」,類型(pid),pid)時會發生什麼?你使用的是什麼Python版本? (例如對於2.x,您可能需要使用不帶括號的打印) – Bigfoot29

+0

請您詳細說明一下,我正在使用python 2.7.6。 – Shubham