【问题标题】:pymysql error message (INSERT INTO function)pymysql 错误信息(INSERT INTO 函数)
【发布时间】:2021-10-01 04:03:16
【问题描述】:

我遇到了错误消息(INVALID SYNTAX),不知道为什么会这样。 请帮忙解决。

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

【问题讨论】:

  • 您刚刚告诉我们您的 MySQL 凭据
  • sql='INSERT...' 语句应该用两个引号括起来
  • 另外,; 在 python 中无效

标签: python mysql pymysql


【解决方案1】:

试试这个。您必须将INSERT INTO ... 包含在" " 中。另外,声明后不要使用;

我建议你应该使用%s。这样,您可以在该列中插入任何值

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-07
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 2010-11-17
    相关资源
    最近更新 更多