Python mysql fetch request

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 someone please help me understand why I am getting this error?

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

+2
source share
3 answers

I think this line has paranas in the wrong place:

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

You use% for the result of execute () and reg.

Change it to:

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

or

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

depending on whether it will perform a param replacement for you.

+3
source

, MySQLDB. execute, python. % S , python. SQL-, MySQLDB . ( % ), .

  • . MySQLDB ( ).
  • a %. , execute.

    self.dbc.execute( " * , reg =% s", (reg,))

+3

:

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

, fetchone ( , , , , )?

for car_info in self.dbc.fetchall():
    ....
+2

All Articles