pymysql error message (INSERT INTO function)

99
July 26, 2021, at 2:50 PM

I encountered error message (INVALID SYNTAX) and have no idea why it happens. Please help to figure it out.

import pymysql
db = pymysql.connect(host='localhost', port=3306, user='root', passwd='xxxxxxxx', db='ecommerce', charset='utf8')
ecommerce = db.cursor()
for index in range(10):
    product_code = 215673140 + index +1
    sql=INSERT INTO product VALUES(str(product_code),'sample data1','sample date2','sample data3');  ----> here's error point. 
    ecommerce.execute(sql)
db.commit()
db.close()
Answer 1

Try this. You have to enclose INSERT INTO ... inside " ". Also, don't use ; after the statement.

I suggest you should use %s. That way, you can insert any value inside that column

import pymysql
db = pymysql.connect(host='localhost', port=3306, user='root', passwd='xxxxxxxx', db='ecommerce', charset='utf8')
ecommerce = db.cursor()
for index in range(10):
    product_code = 215673140 + index +1
    sql = "INSERT INTO product VALUES (%s, %s,%s,%s,%s)"
    val = (str(product_code),'sample data1','sample date2','sample data3')
    ecommerce.execute(sql, val)
db.commit()
db.close()
READ ALSO
How can I format a String number to have commas in Kotlin?

How can I format a String number to have commas in Kotlin?

i have this code to calculate distance from 2 points(lat,lon)

85
How do i select dropdown values which is outside from easyui datagrid table

How do i select dropdown values which is outside from easyui datagrid table

I'm working on the Codeigniter web application where I have used the jquery easyui Datagrid table, I have created the dropdown which is outside from the easyui Datagrid table, now how do I select dropdown value when Datagrid table is loaded?

66
How to keep page content from scrolling without disabling scrolling?

How to keep page content from scrolling without disabling scrolling?

Alright so I'm really stumped by this oneHow can I lock the page's scroll position to a specific value? I can't use overflow: hidden; on the body, because that prevents scroll events from firing entirely

46