适当形成的Python Mysql查询

时间:2015-05-21 18:50:21

标签: mysql python-2.7

我在mysql查询的格式上苦苦挣扎,我希望你能指出我正确的方向。以下是查询

sql = "SELECT price FROM inventory WHERE card_name = %s AND card_set = %s"
sql_rare = "SELECT rarity FROM inventory WHERE card_name = %s AND card_set = %s"
sql_count = "SELECT count(*) FROM inventory WHERE card_name = %s AND card_set = %s

当我运行以下代码时,使用sql_count查询,我收到错误消息:

  

文件" C:\ Users \ Spencer \ Desktop \ Python程序\ PythonMTG \ Revision3AutoAndManual \ 51515 \ magicassistantcsv.py",第264行,在output_card中       对于getmtgprice.query中的行(sql_count,([card_name,set_name])):   TypeError:query()只需要4个参数(给定3个)

以下是产生此错误的代码:

getmtgprice = PriceCheck()
        for row in getmtgprice.query(sql_count, ([card_name, set_name])):  
            if row[0] ==0:
            priced_card = '0.00'

以下是PriceCheck功能:

class PriceCheck(object):
    def __init__(self):
        self.conn = MySQLdb.connect(host='localhost', user='root',     passwd='', db='mscan')
        self.c = self.conn.cursor()

    def query(self, arg, cardname, setname):
        self.c.execute(arg, cardname, setname)
        return self.c

    def __del__(self):
        self.conn.close()

你看到我哪里出错了吗?

1 个答案:

答案 0 :(得分:0)

您的query方法为cardnamesetname采用单独的参数,而不是包含两者的列表。所以,而不是:

for row in getmtgprice.query(sql_count, ([card_name, set_name])): 

你应该:

for row in getmtgprice.query(sql_count, card_name, set_name):