回答編集履歴
6
PyMySQLで動作確認が取れたので変更!
test
CHANGED
@@ -84,6 +84,10 @@
|
|
84
84
|
|
85
85
|
# -*- coding: utf8 -*-
|
86
86
|
|
87
|
+
from contextlib import closing
|
88
|
+
|
89
|
+
import json
|
90
|
+
|
87
91
|
# pip install PyMySQL
|
88
92
|
|
89
93
|
import pymysql
|
@@ -92,17 +96,21 @@
|
|
92
96
|
|
93
97
|
|
94
98
|
|
99
|
+
|
100
|
+
|
95
101
|
def connect():
|
96
102
|
|
97
103
|
return pymysql.connect(host='localhost',
|
98
104
|
|
99
|
-
|
105
|
+
user='root',
|
100
106
|
|
101
|
-
|
107
|
+
password='',
|
102
108
|
|
103
|
-
|
109
|
+
db='testdb',
|
104
110
|
|
105
|
-
|
111
|
+
client_flag=MULTI_STATEMENTS)
|
112
|
+
|
113
|
+
|
106
114
|
|
107
115
|
|
108
116
|
|
@@ -112,11 +120,27 @@
|
|
112
120
|
|
113
121
|
with closing(conn.cursor()) as cur:
|
114
122
|
|
115
|
-
|
123
|
+
cur.execute("select 1;create table sampleA (id int, rnd int);create table sampleB (id int, rnd int);")
|
116
124
|
|
125
|
+
while True:
|
126
|
+
|
127
|
+
print(cur.fetchall())
|
128
|
+
|
129
|
+
if not cur.nextset():
|
130
|
+
|
117
|
-
|
131
|
+
break
|
118
132
|
|
119
133
|
conn.commit()
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
if __name__ == '__main__':
|
140
|
+
|
141
|
+
main()
|
142
|
+
|
143
|
+
|
120
144
|
|
121
145
|
```
|
122
146
|
|
5
追記
test
CHANGED
@@ -82,6 +82,12 @@
|
|
82
82
|
|
83
83
|
```
|
84
84
|
|
85
|
+
# -*- coding: utf8 -*-
|
86
|
+
|
87
|
+
# pip install PyMySQL
|
88
|
+
|
89
|
+
import pymysql
|
90
|
+
|
85
91
|
from pymysql.constants.CLIENT import MULTI_STATEMENTS
|
86
92
|
|
87
93
|
|
4
追記
test
CHANGED
@@ -73,3 +73,47 @@
|
|
73
73
|
|
74
74
|
|
75
75
|
```
|
76
|
+
|
77
|
+
---
|
78
|
+
|
79
|
+
2018/04/16追記
|
80
|
+
|
81
|
+
接続ドライバはPyMySQLだったのですね。動作確認していませんが、こんな感じでどうでしょうか。
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
from pymysql.constants.CLIENT import MULTI_STATEMENTS
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
def connect():
|
90
|
+
|
91
|
+
return pymysql.connect(host='localhost',
|
92
|
+
|
93
|
+
user='admin',
|
94
|
+
|
95
|
+
password='password',
|
96
|
+
|
97
|
+
db='mysql',
|
98
|
+
|
99
|
+
client_flag=MULTI_STATEMENTS)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
def main() -> None:
|
104
|
+
|
105
|
+
with closing(connect()) as conn:
|
106
|
+
|
107
|
+
with closing(conn.cursor()) as cur:
|
108
|
+
|
109
|
+
for _ in cur.execute("create table sampleA (id int, rnd int);create table sampleB (id int, rnd int);"):
|
110
|
+
|
111
|
+
pass
|
112
|
+
|
113
|
+
conn.commit()
|
114
|
+
|
115
|
+
```
|
116
|
+
|
117
|
+
◇参考情報
|
118
|
+
|
119
|
+
[Hang when the second command in a multi-statement fails](https://github.com/PyMySQL/PyMySQL/issues/647)
|
3
全ソースコードを記載&executeをpassで処理するように変更
test
CHANGED
@@ -1,4 +1,14 @@
|
|
1
|
+
◇実行環境
|
2
|
+
|
3
|
+
OS:Windows 10
|
4
|
+
|
5
|
+
Anaconda3
|
6
|
+
|
7
|
+
mysql.connector
|
8
|
+
|
9
|
+
|
10
|
+
|
1
|
-
|
11
|
+
`Cursor#execute`の引数に`multi=True`が必要でした。
|
2
12
|
|
3
13
|
`Create Table`は`list`に入れて`";".join()`するほうが問題が発生しないかもです。
|
4
14
|
|
@@ -6,14 +16,60 @@
|
|
6
16
|
|
7
17
|
|
8
18
|
|
19
|
+
以下はサンプルソースコードです。
|
20
|
+
|
9
21
|
```Python
|
22
|
+
|
23
|
+
# -*- coding: utf8 -*-
|
24
|
+
|
25
|
+
from contextlib import closing
|
26
|
+
|
27
|
+
import json
|
28
|
+
|
29
|
+
# pip install mysql-connector-python-rf
|
30
|
+
|
31
|
+
import mysql.connector
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
def connect():
|
38
|
+
|
39
|
+
with open('settings.json', encoding='utf-8-sig') as f:
|
40
|
+
|
41
|
+
json_data = json.load(f)
|
42
|
+
|
43
|
+
con_str = json_data['connection_strings']
|
44
|
+
|
45
|
+
print(con_str)
|
46
|
+
|
47
|
+
return mysql.connector.connect(**con_str)
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
def main() -> None:
|
10
54
|
|
11
55
|
with closing(connect()) as conn:
|
12
56
|
|
13
57
|
with closing(conn.cursor()) as cur:
|
14
58
|
|
15
|
-
cur.execute(
|
59
|
+
for _ in cur.execute("create table sampleA (id int, rnd int);create table sampleB (id int, rnd int);", multi=True):
|
60
|
+
|
61
|
+
pass
|
16
62
|
|
17
63
|
conn.commit()
|
18
64
|
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
if __name__ == '__main__':
|
70
|
+
|
71
|
+
main()
|
72
|
+
|
73
|
+
|
74
|
+
|
19
75
|
```
|
2
変更!
test
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
Windows 10&mysql.connectorの環境で試してみましたが、Cursor#executeの引数にmulti=Trueが必要でした。
|
1
|
+
Windows 10&mysql.connectorの環境で試してみましたが、`Cursor#execute`の引数に`multi=True`が必要でした。
|
2
2
|
|
3
|
-
Create Tableはlistに入れて";".join()するほうが問題が発生しないかもです。
|
3
|
+
`Create Table`は`list`に入れて`";".join()`するほうが問題が発生しないかもです。
|
4
4
|
|
5
|
-
sqlをprintして手で実行して確認してみてはどうでしょうか。
|
5
|
+
実行sqlを`print`して手で実行して確認してみてはどうでしょうか。
|
6
6
|
|
7
7
|
|
8
8
|
|
1
追記
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Windows 10環境で試してみましたが、Cursor#executeの引数にmulti=Trueが必要でした。
|
1
|
+
Windows 10&mysql.connectorの環境で試してみましたが、Cursor#executeの引数にmulti=Trueが必要でした。
|
2
2
|
|
3
3
|
Create Tableはlistに入れて";".join()するほうが問題が発生しないかもです。
|
4
4
|
|