无法使用插入数据python.mysql康涅狄格州

2024-04-25 01:58:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始使用MySQL connector for python,我将下面给定的数据插入名为“region_-based_-billings”的表中。我尝试了所有可能的方法来插入这些数据,但似乎我缺少一个语法,不确定确切的位置和数据类型。在

   date  = '2016-06-29'
   region = 'Asia Pacific'
   account_type = 'PRODUCTION ACCOUNT'
   product_type  = 'AmazonEC2'
   total= 10.58383305
   count = 21

   cursor = self.testdb.cursor()
   cursor.execute('INSERT INTO iaas.region_based_billing (date, region, account type, product type, total amount, no of products) VALUES (%s, %s, %s, %s, %s, %s)',(date, region, account_type,product_type,total,count) )

    self.testdb.commit()
    self.testdb.close()

我的表对象是:

^{pr2}$

我知道这很基本,不知道到底是什么引起了问题。我尝试为日期类型“%s”保留单引号,但仍然不起作用。在

这里还有一个错误

mysql.connector.errors.ProgrammingError:1064(42000):您的SQL语法有错误;请查看与您的MySQL server版本相对应的手册,了解正确的语法,以使用“type,product type,total amount,no of products”值(“2016-06-29”,“Asia Pa”第1行)


Tags: 数据selfconnectordatetypemysql语法account
3条回答

这里没有产品的数据类型是int。但是,您使用的是一个字符串。对于整数,需要使用%d。在

在光标.执行(插入iaas.region_-based_计费(日期、地区、账户类型、产品类型、总额、产品数量)值(%s、%s、%s、%f、%d),(日期、地区、计数类型、产品类型、合计、计数)

You can check this example.

去掉了空白,解决了问题。谢谢。在

Serial_no       int(11) NO  PRI     auto_increment
date            date    NO          
region          varchar(50) NO          
account_type    varchar(65) NO          
product_type    varchar(65) NO          
total_amount    float   YES         
no_of_products  int(255)    NO          

删除列名之间的空格。列名应为一个或多个由“”分隔的单词。 在您的例子中,列名有间隙。所以这些列应该是:

account type = account_type
product type = product_type

等等。。。在

相关问题 更多 >