无法将日期插入SQL表

时间:2016-11-22 19:55:29

标签: sql vba odbc

我试图在Prestashop SQL数据库中插入一条新记录,一切正常,直到我需要引入日期记录。

如果我尝试执行此行,则会收到错误消息"您的SQL语法错误"

INSERT INTO ps_specific_price 
    (id_product, [reduction], [reduction_type]
                    , [from], [to]) 
 VALUES (100145, 0.15, "percentage"
                    , "2016-10-10 00:00:00", "2017-10-10 00:00:00")

如果我使用相同但没有日期的话,它可以正常工作:

INSERT INTO ps_specific_price 
      (id_product, [reduction], [reduction_type]) 
  VALUES (100145, 0.15, "percentage")

我使用带有ODBC连接器和MySQL ODBC 5.3 ANSI驱动程序的VBA代码。

我的代码出了什么问题? 我尝试过不同的时间格式,有小时印章和不带小时印章......不工作......

谢谢!

这是架构:

'id_specific_price' int(10) unsigned NOT NULL AUTO_INCREMENT,
'id_specific_price_rule' int(11) unsigned NOT NULL,
'id_cart' int(11) unsigned NOT NULL,
'id_product' int(10) unsigned NOT NULL,
'id_shop' int(11) unsigned NOT NULL DEFAULT '1',
'id_shop_group' int(11) unsigned NOT NULL,
'id_currency' int(10) unsigned NOT NULL,
'id_country' int(10) unsigned NOT NULL,
'id_group' int(10) unsigned NOT NULL,
'id_customer' int(10) unsigned NOT NULL,
'id_product_attribute' int(10) unsigned NOT NULL,
'price' decimal(20,6) NOT NULL,
'from_quantity' mediumint(8) unsigned NOT NULL,
'reduction' decimal(20,6) NOT NULL,
'reduction_tax' tinyint(1) NOT NULL DEFAULT '1',
'reduction_type' enum('amount','percentage') NOT NULL,
'from' datetime NOT NULL,
'to' datetime NOT NULL,
PRIMARY KEY ('id_specific_price'),
UNIQUE KEY 'id_product_2' ('id_cart','id_product','id_shop','id_shop_group','id_currency','id_country','id_group','id_customer','id_product_attribute','from_quantity','id_specific_price_rule','from','to'),
KEY 'id_product' ('id_product','id_shop','id_currency','id_country','id_group','id_customer','from_quantity','from','to'),
KEY 'from_quantity' ('from_quantity'),
KEY 'id_specific_price_rule' ('id_specific_price_rule'),
KEY 'id_cart' ('id_cart')

2 个答案:

答案 0 :(得分:0)

试试这个

 INSERT INTO ps_specific_price 
   (id_product, [reduction], [reduction_type]
                , [from], [to]) 
VALUES (100145, 0.15, "percentage"
                  , cast ( "2016-10-10 00:00:00" as datetime), cast ("2017-10-10 00:00:00" as datetime )

答案 1 :(得分:0)

知道了! 我刚刚在phpmyadmin中看到了这条消息:“列名'to'是MySQL保留的关键字。”

所以我检查了它并将“from”改为“backtick from backtick”,如Syntax error due to using a reserved word as a table or column name in MySQL所示。

谢谢!