【问题标题】:Insert Ordered Dict values while using MySQL INSERT INTO在使用 MySQL INSERT INTO 时插入 Ordered Dict 值
【发布时间】:2016-12-06 16:15:10
【问题描述】:

我在尝试向表中插入一些值时遇到此错误。 这是我的代码:

def tsx_insert(self, d_list):

for item in d_list:

    query = """ INSERT IGNORE INTO tsx_first_insert(protocollo,procedura,oggetto,priorita,
                                                    tipo_richiesta,sottotipo_richiesta,emergenza,
                                                    richiesta,uo_richiedente,autore,scadenza_sla)
                VALUES(%(protocollo)s,%(procedura)s,%(oggetto)s,%(priorita)s,%(tipo_richiesta)s,
                       %(sottotipo_richiesta)s,%(emergenza)s,%(richiesta)s,%(uo_richiedente)s,
                       %(autore)s,%(scadenza_sla)s)"""

    values = item.values()

    self.exec_query(query,values)

这里还有'exec_query'函数:

def exec_query(self, query, params):
    try:
        if self.connected is None:
            self.connect()

        self.cursor = self.connected.cursor()
        self.cursor.connection.autocommit(True)
        self.cursor.execute(query)

        if self.cursor.description:
            self.description = [d[0] for d in self.cursor.description]

        self.rows = self.cursor.rowcount
        self.sql_result = self.cursor.fetchall()
    except MySQLdb.Error, e:
        logging.error('Error {0}: {1}'.format(e.args[0], e.args[1]))
    finally:
        self.cursor.close()

错误是:“错误 1064:您的 SQL 语法有错误;请查看与您的 MariaDB 服务器版本相对应的手册,了解在 '%(protocollo)s,%(procedura)s, 附近使用的正确语法, %(oggetto)s,%(priorita)s,%(tipo_richiesta)s, ' 在第 4 行"

我不知道是什么问题。提前感谢您的帮助。

【问题讨论】:

  • 您遗漏了错误中最重要的部分
  • @e4c5 已编辑完整的错误消息
  • 看起来你没有代入参数...

标签: python mysql mysql-python mysql-error-1064


【解决方案1】:

您忘记在您的 self.cursor.execute() 方法调用中提及您的 params 字典,因此参数字符串留在原处而不是替换。

试试

   self.cursor.execute(query, params) 

【讨论】:

  • 请注意,values 变量将变得完全多余!
  • 那是个错误,谢谢。但是,我遇到了另一个错误: query = query % tuple([db.literal(item) for item in args]) ---> TypeError: format requires a mapping
猜你喜欢
  • 2018-10-30
  • 1970-01-01
  • 2022-07-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-07
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
相关资源
最近更新 更多