mysql - Whats wrong with this python sql statement -
i have db schema in mysql
id int(11) pk ai apt_id varchar(200) checkin_date date checkout_date date price decimal(10,0) deposit decimal(10,0) adults int(11) source_id int(11) confirmationcode varchar(100) client_id int(11) booking_date datetime note mediumtext related tables:property (apt_id → apt_id) booking_source (source_id → id)
i trying insert value in db using following query
self.start_at = datetime.strptime(self.start_at[0:10] + ' ' + self.start_at[11:19], "%y-%m-%d %h:%m:%s") self.end_at = datetime.strptime(self.end_at[0:10] + ' ' + self.end_at[11:19], "%y-%m-%d %h:%m:%s") x = db.cursor() sql = """insert `nycaptbs`.`booking` (`apt_id`, `checkin_date`, `checkout_date`, `price`,`deposite` `adults`, `source_id`, `confirmationcode`, `client_id`, `booking_date`) values ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s' )""" x.execute(sql,(self.apt_id,self.start_at,self.end_at,self.final_price,self.deposit,self.adults,self.source_id,self.notes,self.client_id,self.booking_date,self.notes))
since error not clear
file "/usr/lib/python2.7/dist-packages/mysqldb/cursors.py", line 159, in execute query = query % db.literal(args) typeerror: not enough arguments format string
please me out how can resolve .i worked lot on django orm have write in mysql query .
thanks
this exception raised when have more formatting codes in string, , not enough arguments passed in:
>>> s = "hello %s %s %s" >>> print(s % ('a','b')) traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: not enough arguments format string
here see have 3 %s
, passing in 2 strings ('a','b')
.
you have same problem query, because missing comma:
sql = """ insert `nycaptbs`.`booking` ( `apt_id`, `checkin_date`, `checkout_date`, `price`, `deposite` `adults`, # missing comma here `source_id`, `confirmationcode`, `client_id`, `booking_date`) values ( '%s', #1 '%s', #2 '%s', #3 '%s', #4 '%s', #5 '%s', #6 '%s', #7 '%s', #8 '%s', #9 '%s' #10 ) """ x.execute(sql,( self.apt_id, #1 self.start_at, #2 self.end_at, #3 self.final_price, #4 self.deposit, #5 self.adults, #6 self.source_id, #7 self.notes, #8 self.client_id, #9 self.booking_date, #10 self.notes #11 ))
Comments
Post a Comment