2015-08-19 43 views
2

呼叫:SQL語法錯誤在荒謬簡單的SQL?

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 

我曾與一個名爲PARAMS試過,?%s,並獲得

  • 並非所有的參數使用
  • 的參數錯誤號碼命名的參數
  • SQL語法

這是怎麼回事?我錯過了什麼?

+0

當你用'%(組名)'應該不是替換是在字典而不是列表中? – Barmar

+0

'dict(zip(group_columns,group))'將它們變成一個字典(用正確的鍵名稱)。還有其他的東西也是問題,因爲我不能做list +'%s'或'''格式。 –

+0

你可以粘貼你的Python腳本,以便我們可以查看縮進等? – FirebladeDan

回答

2

它看起來像一個小錯字。你忘了添加s到PARAMS

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

相關的代碼是在這裏:

+0

這插入,排序,但我得到的行看起來像︰http://cl.ly/image/0l1l2d3V181o - 這是記錄在哪裏?我對Mysql,Mysql-python和mysqldb的文檔感到困惑,它們都以不同的方式做到這一點。 –

+1

你的'def row_to_group_values(row)'返回字符串,而不是列表。當你用'group_columns'壓縮它時,你會看到你所看到的。 – twil

+0

我是個大人物。多謝你們。 –