Pythonでmysqlconnectorを使ってデータを挿入したいのですが、たとえば
hc pc
という項目があるデータベースに
123 123
234 234
234 234
234 234
234 234
123 123
234 234
234 234
234 234
234 234
というデータを挿入したい場合どのようにプラグラムを書けば良いのでしょうか?
connect
1 user='root', 2 password='', 3 host='xxxx') 4cursor = connect.cursor() 5comma = ',' 6for j in range(1,3): 7 sql = '\'insert into database (hc,pc) values ' 8 9 10 for i in range(5*(j-1),5*(j-1) + 1): 11 sql2 = '(123,123)' 12 sql = sql + sql2 13 14 15 for i in range(5*(j-1) + 1,5*j): 16 sql2 = '(234,234)' 17 sql = sql + comma + sql2 18 sql = sql + '\'' 19 print sql 20 cursor.execute(sql) 21 connect.commit() 22cursor.close() 23connect.close()
のように書いたのですがエラーになってしまいました。
エラーを読もうと思ったのですがなにやらよくわかりません
Traceback (most recent call last): File "test111.py", line 75, in <module> cursor.execute(sql) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mysql/connector/cursor.py", line 507, in execute self._handle_result(self._connection.cmd_query(stmt)) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mysql/connector/connection.py", line 722, in cmd_query result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query)) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.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 ''insert into database (hc,pc) values (123,123),(234,234),(234,234),(234,234' at line 1 [Finished in 0.1s with exit code 1]
どなたかよろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/10 07:27
2016/09/10 09:15
2016/09/10 15:37