0

I have A 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 am trying to 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 INTO `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 itself is 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 for format string

PLease help me out how can i resolve this .I worked a lot on Django ORM but for this i have to write in Mysql query .

Thanks

4
  • In x.execute you only provide four arguments instead of the 10 you have in the query Commented Apr 18, 2013 at 5:36
  • wait i will update my question Commented Apr 18, 2013 at 5:40
  • Still you set 10 placeholders but pass 11 values Commented Apr 18, 2013 at 5:53
  • @burning, I was trying to help you make your code snippet more readable, and was wondering: are you missing a comma between deposite and adults in your SQL statement? Commented Apr 18, 2013 at 5:54

1 Answer 1

1

This exception is raised when you have more formatting codes in your string, and 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 for format string

Here you see I have three %s, but I am only passing in two strings ('a','b').

You have the same problem with your query, because you are missing a comma:

sql = """
    INSERT INTO `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 ))
Sign up to request clarification or add additional context in comments.

5 Comments

Khalid you are ritgh i was missing comma thank but after that i am getting this error _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2013-04-11 16:00:00'',\n
This is because your database type is DATE but you are passing in a string with date and time.
start_at and end_at are the python date object you can check in question then y
You are sending a string : '2013-04-11 16:00:00' (not a date object), and it is not in the format that a DATE column in MySQL would expect.
@mysql accept in this format check this dev.mysql.com/doc/refman/5.1/en/datetime.html if I am wrong then please tell me the correct format so that i can modify the code Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.