python mysql获取查询 [英] python mysql fetch query

查看:80
本文介绍了python mysql获取查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def dispcar ( self, reg ):
                print ("The car information for '%s' is: "), (reg)
                numrows = int(self.dbc.rowcount) #get the count of total rows
                self.dbc.execute("select * from car where reg='%s'") %(reg)
                for x in range(0, numrows):
                    car_info = self.dbc.fetchone()
                    print row[0], "-->", row[1]

上面的代码给出了此错误:

the above code gives this error:

self.dbc.execute("select * from car where reg='%s' " %(reg)
TypeError: unsupported operand type(s) for %: 'long' and 'str'

任何人都可以帮助我了解我为什么会收到此错误吗?

can anyone please help me understand why am i getting this error?

仅供参考:reg是用户在getitem函数中输入的raw_input var,并将reg var作为该函数的参数传递.

FYI: reg is a raw_input var i input from user in the function getitem and pass the reg var as an argument to this function.

推荐答案

我认为这一行只是将括号放在错误的位置:

I think this line simply has the parens in the wrong place:

self.dbc.execute("select * from car where reg='%s'") %(reg)

您正在对execute()和reg的结果使用%.

You are using % on the result of execute(), and reg.

将其更改为:

self.dbc.execute("select * from car where reg='%s'" % reg)

self.dbc.execute("select * from car where reg='%s'", reg)

取决于是否会替您进行参数替换.

depending on whether it will do the param substitution for you.

这篇关于python mysql获取查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆