荒谬简单的SQL中的SQL语法错误?

时间:2015-08-19 19:40:54

标签: python mysql prepared-statement named-parameters

致电:

INSERT IGNORE INTO standard_group (group_name, subject, grade, country, state) VALUES (%(group_name), %(subject), %(grade), %(country), %(state));

('Arizona Social Studies Standards', 'Social Studies', '1', 'United States', 'AZ')

例如:

    print group_columns
    print group_sql
    print group
    cur.execute(group_sql, dict(zip(group_columns, group)))

但我总是得到:

['group_name', 'subject', 'standard_category', 'standard_sub_category', 'standard_name', 'standard_description', 'grade', 'country', 'state', 'is_common_core', 'is_state_standard']
INSERT IGNORE INTO standard_group (group_name, subject, grade, country, state) VALUES (%(group_name), %(subject), %(grade), %(country), %(state));
('Arizona Social Studies Standards', 'Social Studies', '1', 'United States', 'AZ')
Traceback (most recent call last):
  File "standards-import/simport.py", line 167, in <module>
    cur.execute(group_sql, dict(zip(group_columns, group)))
  File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 507, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 722, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "/Library/Python/2.7/site-packages/mysql/connector/connection.py", line 640, in _handle_result
    raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%(group_name), %(subject), %(grade), %(country), %(state))' at line 1

我尝试使用命名参数?%s,然后获取

  • 并非所有参数都使用
  • 命名参数
  • 的参数数量错误
  • SQL语法

发生什么事了?我错过了什么???

1 个答案:

答案 0 :(得分:2)

这看起来像一个小错字。您忘记将s添加到参数

INSERT IGNORE INTO standard_group (group_name, subject, grade, country, state) VALUES (%(group_name)s, %(subject)s, %(grade)s, %(country)s, %(state)s);

相关代码在这里: