2013-04-18 40 views
0

我有MySQL中的数据库架构哪些错误与这条巨蟒的SQL语句

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) 

我试图用插入在DB值以下查询

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)) 

由于错误本身是不明确

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 

请帮我解决这个问题。我在Django ORM上工作了很多,但是为此我必须在Mysql查询中编写代码。

感谢

+0

在'x.execute'你只提供四个参数,而不是10,你在查询 – 2013-04-18 05:36:14

+0

等待我会更新我的问题 – burning 2013-04-18 05:40:24

+0

仍然设置了10个占位符,但传递了11个值 – 2013-04-18 05:53:14

回答

1

当你在传入您的字符串更多的格式代码,并没有足够的论据,引发此异常:

>>> 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 

这里你可以看到我有三个%s,但我只是路过在两个字符串('a','b')

你必须与你的查询同样的问题,因为你缺少一个逗号:

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)) 
+0

Khalid你是ritgh我错过了逗号感谢,但之后,我得到这个错误_mysql_exceptions.ProgrammingError:(1064,“你有一个错误在你的SQL语法;检查手册,对应于你的MySQL服务器版本的正确的语法在'2013-04-11 16:00:00'附近使用,\ n – burning 2013-04-18 06:35:46

+0

这是因为你的数据库类型是'DATE',但你传入的日期和时间是字符串 – 2013-04-18 06:39:28

+0

start_at和end_at是python日期对象您可以检查问题,然后y – burning 2013-04-18 07:12:47