質問編集履歴

2

誤字

2016/09/09 16:19

投稿

u_k_statistics
u_k_statistics

スコア44

test CHANGED
File without changes
test CHANGED
@@ -8,9 +8,7 @@
8
8
 
9
9
  という項目があるデータベースに
10
10
 
11
-
12
-
13
- 123 123
11
+ 123 123
14
12
 
15
13
  234 234
16
14
 

1

問題点をより明確にさせていただきました

2016/09/09 16:19

投稿

u_k_statistics
u_k_statistics

スコア44

test CHANGED
File without changes
test CHANGED
@@ -1,23 +1,119 @@
1
1
  Pythonでmysqlconnectorを使ってデータを挿入したいのですが、たとえば
2
2
 
3
- a b c d
4
3
 
5
- という4つの項目があるデータベースに一気に
6
4
 
7
- 11 12 13 14
5
+ hc pc
8
6
 
7
+
8
+
9
+ という項目があるデータベースに
10
+
11
+
12
+
13
+ 123 123
14
+
9
- 21 22 23 24
15
+ 234 234
16
+
17
+ 234 234
18
+
19
+ 234 234
20
+
21
+ 234 234
22
+
23
+ 123 123
24
+
25
+ 234 234
26
+
27
+ 234 234
28
+
29
+ 234 234
30
+
31
+ 234 234
32
+
33
+
10
34
 
11
35
  というデータを挿入したい場合どのようにプラグラムを書けば良いのでしょうか?
12
36
 
13
- 私なりに
37
+ ```connect = mysql.connector.connect(
14
38
 
15
- cursor.execute('insert into database (a,b,c,d) values (%s,%s,%s,%s)',(11,12,13,14),(21,22,23,24))
39
+ user='root',
16
40
 
17
-
41
+ password='',
18
42
 
19
- cursor.execute('insert into database (a,b,c,d) values (%s,%s,%s,%s),(%s,%s,%s,%s)',(11,12,13,14),(21,22,23,24))
43
+ host='xxxx')
20
44
 
21
- などとかいて行ってみたのですがうまくいきませんでした。
45
+ cursor = connect.cursor()
46
+
47
+ comma = ','
48
+
49
+ for j in range(1,3):
50
+
51
+ sql = '\'insert into database (hc,pc) values '
52
+
53
+
54
+
55
+
56
+
57
+ for i in range(5*(j-1),5*(j-1) + 1):
58
+
59
+ sql2 = '(123,123)'
60
+
61
+ sql = sql + sql2
62
+
63
+
64
+
65
+
66
+
67
+ for i in range(5*(j-1) + 1,5*j):
68
+
69
+ sql2 = '(234,234)'
70
+
71
+ sql = sql + comma + sql2
72
+
73
+ sql = sql + '\''
74
+
75
+ print sql
76
+
77
+ cursor.execute(sql)
78
+
79
+ connect.commit()
80
+
81
+ cursor.close()
82
+
83
+ connect.close()
84
+
85
+ ```
86
+
87
+ のように書いたのですがエラーになってしまいました。
88
+
89
+ エラーを読もうと思ったのですがなにやらよくわかりません
90
+
91
+ ```
92
+
93
+ Traceback (most recent call last):
94
+
95
+ File "test111.py", line 75, in <module>
96
+
97
+ cursor.execute(sql)
98
+
99
+ File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mysql/connector/cursor.py", line 507, in execute
100
+
101
+ self._handle_result(self._connection.cmd_query(stmt))
102
+
103
+ File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mysql/connector/connection.py", line 722, in cmd_query
104
+
105
+ result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
106
+
107
+ File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mysql/connector/connection.py", line 640, in _handle_result
108
+
109
+ raise errors.get_exception(packet)
110
+
111
+ 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
112
+
113
+
114
+
115
+ [Finished in 0.1s with exit code 1]
116
+
117
+ ```
22
118
 
23
119
  どなたかよろしくお願い致します。