Python mysql 错误#1064

标签 python mysql mysql-error-1064

我试图通过写入管道将数据存储到MySQL中,但它发生了:

_mysql_exceptions.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 'desc,pic)values ('xe4\xbe\x9b\xe5\xba.......' at line 1")

这是我的代码:

def __init__(self):
    #self.file = codecs.open('tutorial_data.json','wb',encoding='utf-8')
    self.dbpool = adbapi.ConnectionPool('MySQLdb',
                                        host = 'localhost',
                                        port = 3306,
                                        db = 'project',
                                        user = 'root',
                                        passwd = '',
                                        cursorclass = MySQLdb.cursors.DictCursor,
                                        charset = 'utf8',
                                        use_unicode = True)
def process_item(self, item, spider):
    query = self.dbpool.runInteraction(self._conditional_insert, item)
    query.addErrback(self.handle_error)
    return item

def _conditional_insert(self,tx,item):

        tx.execute(
        "INSERT INTO raw (title,area,date,sclass,desc,pic )\ 
         VALUES (%s, %s, %s, %s, %s, %s)",

               (item['title'][0],
                item['area'][0],
                item['date'][0],
                item['sclass'][0],
                item['desc'][0],
                item['pic'][0])
                )    

我的MySQL版本是5.6.17,mysql-python版本是1.2.5_

最佳答案

DESCreserved keyword in MySQL 。如果您要命名列标识符 DESC,您必须将其包含在刻度中:

 tx.execute(
    "INSERT INTO raw (title,area,date,sclass,`desc`,pic )\ 
     VALUES (%s, %s, %s, %s, %s, %s)",

但是,更好的解决方案是不要使用保留关键字作为列标识符,因为它可能会在其他地方引起困惑和潜在的错误(加上完整的“描述”一词更清晰,并使代码更易于维护)。

 tx.execute(
    "INSERT INTO raw (title,area,date,sclass,description,pic )\ 
     VALUES (%s, %s, %s, %s, %s, %s)",

关于Python mysql 错误#1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29145601/

相关文章:

python - 来自 py2exe.mf3._scan_opcodes 的 "tuple index out of range"是什么意思?

Python 请求库 : use of json. 转储

python - Django modelform 中的某些字段未更新

已使用连接表的语句上的 mySQL 行号生成

MySQL 每个派生表都必须有自己的别名

MySQL AVG 函数给出的小数比预期多

sql - mysqldump 命令不在 mysql shell 中运行

python - Pandas 将数据帧与不同的数据合并以实现特定的输出

MySQL 在存储过程中抛出一个错误 IF 函数

c# - MySQL 在 C# 中以编程方式触发