I'm new to using MySQL connector for python and I have the below-given data to be inserted into a table called "region_based_billings". I tried all possible ways to insert this data and it seems I missing a syntax not sure exactly where and for which data type? .
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()
My table objects are :
desc iaas.region_based_billing;
date date NO PRI
region varchar(50) NO
account type varchar(65) NO
product type varchar(65) NO
total amount float NO
no of products int(255) NO
I know it's pretty basic, not sure what exactly causing the problem. I tried keeping single quotes for date type '%s', but still it didn't work.
Also here is the error
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 'type, product type, total amount, no of products) VALUES ('2016-06-29', 'Asia Pa' at line 1
Remove space between column names. column name should be a single word or words seprated by _. In your case column name have gap. So these column should be:-
account type = account_type
product type = product_type
and so on...
Removed the white spaces and it resolved the problem. Thanks.
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
Here no of products has a datatype of int. But, you are using it has a string. For integer, you need to use %d .
cursor.execute('INSERT INTO iaas.region_based_billing (date, region, account type, product type, total amount, no of products) VALUES (%s, %s, %s, %s, %f, %d)' ,(date, region, count_type,product_type,total,count) )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.