如何解决 pymysql.err.ProgrammingError: 1064 - 在 mysql 上保存数据?

Posted

技术标签:

【中文标题】如何解决 pymysql.err.ProgrammingError: 1064 - 在 mysql 上保存数据?【英文标题】:How to resolve pymysql.err.ProgrammingError: 1064 - save data on mysql? 【发布时间】:2019-01-10 15:07:41 【问题描述】:

我正在尝试将使用 scrapy 抓取的数据保存到 mysql。但是,我有这些问题:

    不再支持MySQLdb。所以,我必须使用

    import pymysql

    pymysql.install_as_MySQLdb()settings.py 文件

    在 python 3 上,%s 已弃用,我必须使用带有以下 代码的.format:

def close(self, reason):
        csv_file = max(glob.iglob('*.csv'), key=os.path.getctime)       
        mydb = MySQLdb.connect(host='localhost',
                               user='demo',
                               passwd='123456',
                               db='testdb')
        cursor = mydb.cursor()

        csv_data = csv.reader(open(csv_file))

        row_count = 0
        for row in csv_data:
            if row_count != 0:
                cursor.execute("INSERT IGNORE INTO testtb(product, category) VALUES('','')".format(*row))
            row_count += 1

        mydb.commit()
        cursor.close()

我有以下错误

<bound method AutorSpider.close of <AutorSpider 'autor' at 0x7f64725d29b0>>

Traceback (most recent call last):
  File "/home/pc/.local/lib/python3.6/site-packages/twisted/internet/defer.py", line 151, in maybeDeferred
    result = f(*args, **kw)
  File "/home/pc/.local/lib/python3.6/site-packages/pydispatch/robustapply.py", line 55, in robustApply
    return receiver(*arguments, **named)
  File "/home/pc/Escritorio/fpyautor/fpyautor/spiders/autor.py", line 109, in close
    cursor.execute("INSERT IGNORE INTO autortb(frase, categoria) VALUES(,'')'".format(*row))
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
    result = self._query(query)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
    conn.query(q)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 727, in _read_query_result
    result.read()
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/connections.py", line 683, in _read_packet
    packet.check_error()
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "/home/pc/.local/lib/python3.6/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "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 'titulo del item numero 1' at line 1")

还有其他更简单/有效的方法吗?因为我在抓取任务结束时保存了数据,如果我得到更多结果(3000 个项目),这可能是未来更大站点的问题?

【问题讨论】:

product varchar 列吗? 好的,感谢您的评论 而且你的牙套没有正确闭合。 已编辑,已编辑。 等等,你还是得到错误? 【参考方案1】:

escape string可以帮你

def close(self, reason):
    csv_file = max(glob.iglob('*.csv'), key=os.path.getctime)       
    mydb = MySQLdb.connect(host='localhost',
                           user='demo',
                           passwd='123456',
                           db='testdb')
    cursor = mydb.cursor()

    csv_data = csv.reader(open(csv_file))

    row_count = 0
    for row in csv_data:
        if row_count != 0:
            product = mydb.escape_string(row[0])
            category = mydb.escape_string(row[1])
            #print category , product
            sql = 'INSERT IGNORE INTO testtb(product, category) VALUES ( "","")'.format(product,category)
            #print sql
            cursor.execute(sql)
            row_count += 1

    mydb.commit()
    cursor.close()

【讨论】:

以上是关于如何解决 pymysql.err.ProgrammingError: 1064 - 在 mysql 上保存数据?的主要内容,如果未能解决你的问题,请参考以下文章

如何解决 Ajax 跨域请求不到的问题

如何解决包冲突问题

如何解决包冲突问题

如何解决ajax跨域问题

MySQL 的 10048问题,如何解决?

如何解决smartgit的冲突问题