回答編集履歴
6
PyMySQLで動作確認が取れたので変更!
answer
CHANGED
@@ -41,23 +41,35 @@
|
|
41
41
|
接続ドライバはPyMySQLだったのですね。動作確認していませんが、こんな感じでどうでしょうか。
|
42
42
|
```
|
43
43
|
# -*- coding: utf8 -*-
|
44
|
+
from contextlib import closing
|
45
|
+
import json
|
44
46
|
# pip install PyMySQL
|
45
47
|
import pymysql
|
46
48
|
from pymysql.constants.CLIENT import MULTI_STATEMENTS
|
47
49
|
|
50
|
+
|
48
51
|
def connect():
|
49
52
|
return pymysql.connect(host='localhost',
|
50
|
-
|
53
|
+
user='root',
|
51
|
-
|
54
|
+
password='',
|
52
|
-
|
55
|
+
db='testdb',
|
53
|
-
|
56
|
+
client_flag=MULTI_STATEMENTS)
|
54
57
|
|
58
|
+
|
55
59
|
def main() -> None:
|
56
60
|
with closing(connect()) as conn:
|
57
61
|
with closing(conn.cursor()) as cur:
|
58
|
-
|
62
|
+
cur.execute("select 1;create table sampleA (id int, rnd int);create table sampleB (id int, rnd int);")
|
63
|
+
while True:
|
64
|
+
print(cur.fetchall())
|
65
|
+
if not cur.nextset():
|
59
|
-
|
66
|
+
break
|
60
67
|
conn.commit()
|
68
|
+
|
69
|
+
|
70
|
+
if __name__ == '__main__':
|
71
|
+
main()
|
72
|
+
|
61
73
|
```
|
62
74
|
◇参考情報
|
63
75
|
[Hang when the second command in a multi-statement fails](https://github.com/PyMySQL/PyMySQL/issues/647)
|
5
追記
answer
CHANGED
@@ -40,6 +40,9 @@
|
|
40
40
|
2018/04/16追記
|
41
41
|
接続ドライバはPyMySQLだったのですね。動作確認していませんが、こんな感じでどうでしょうか。
|
42
42
|
```
|
43
|
+
# -*- coding: utf8 -*-
|
44
|
+
# pip install PyMySQL
|
45
|
+
import pymysql
|
43
46
|
from pymysql.constants.CLIENT import MULTI_STATEMENTS
|
44
47
|
|
45
48
|
def connect():
|
4
追記
answer
CHANGED
@@ -35,4 +35,26 @@
|
|
35
35
|
if __name__ == '__main__':
|
36
36
|
main()
|
37
37
|
|
38
|
-
```
|
38
|
+
```
|
39
|
+
---
|
40
|
+
2018/04/16追記
|
41
|
+
接続ドライバはPyMySQLだったのですね。動作確認していませんが、こんな感じでどうでしょうか。
|
42
|
+
```
|
43
|
+
from pymysql.constants.CLIENT import MULTI_STATEMENTS
|
44
|
+
|
45
|
+
def connect():
|
46
|
+
return pymysql.connect(host='localhost',
|
47
|
+
user='admin',
|
48
|
+
password='password',
|
49
|
+
db='mysql',
|
50
|
+
client_flag=MULTI_STATEMENTS)
|
51
|
+
|
52
|
+
def main() -> None:
|
53
|
+
with closing(connect()) as conn:
|
54
|
+
with closing(conn.cursor()) as cur:
|
55
|
+
for _ in cur.execute("create table sampleA (id int, rnd int);create table sampleB (id int, rnd int);"):
|
56
|
+
pass
|
57
|
+
conn.commit()
|
58
|
+
```
|
59
|
+
◇参考情報
|
60
|
+
[Hang when the second command in a multi-statement fails](https://github.com/PyMySQL/PyMySQL/issues/647)
|
3
全ソースコードを記載&executeをpassで処理するように変更
answer
CHANGED
@@ -1,10 +1,38 @@
|
|
1
|
+
◇実行環境
|
2
|
+
OS:Windows 10
|
3
|
+
Anaconda3
|
4
|
+
mysql.connector
|
5
|
+
|
1
|
-
|
6
|
+
`Cursor#execute`の引数に`multi=True`が必要でした。
|
2
7
|
`Create Table`は`list`に入れて`";".join()`するほうが問題が発生しないかもです。
|
3
8
|
実行sqlを`print`して手で実行して確認してみてはどうでしょうか。
|
4
9
|
|
10
|
+
以下はサンプルソースコードです。
|
5
11
|
```Python
|
12
|
+
# -*- coding: utf8 -*-
|
13
|
+
from contextlib import closing
|
14
|
+
import json
|
15
|
+
# pip install mysql-connector-python-rf
|
16
|
+
import mysql.connector
|
17
|
+
|
18
|
+
|
19
|
+
def connect():
|
20
|
+
with open('settings.json', encoding='utf-8-sig') as f:
|
21
|
+
json_data = json.load(f)
|
22
|
+
con_str = json_data['connection_strings']
|
23
|
+
print(con_str)
|
24
|
+
return mysql.connector.connect(**con_str)
|
25
|
+
|
26
|
+
|
27
|
+
def main() -> None:
|
6
28
|
with closing(connect()) as conn:
|
7
29
|
with closing(conn.cursor()) as cur:
|
8
|
-
cur.execute(
|
30
|
+
for _ in cur.execute("create table sampleA (id int, rnd int);create table sampleB (id int, rnd int);", multi=True):
|
31
|
+
pass
|
9
32
|
conn.commit()
|
33
|
+
|
34
|
+
|
35
|
+
if __name__ == '__main__':
|
36
|
+
main()
|
37
|
+
|
10
38
|
```
|
2
変更!
answer
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
Windows 10&mysql.connectorの環境で試してみましたが、Cursor#executeの引数にmulti=Trueが必要でした。
|
1
|
+
Windows 10&mysql.connectorの環境で試してみましたが、`Cursor#execute`の引数に`multi=True`が必要でした。
|
2
|
-
Create Tableはlistに入れて";".join()するほうが問題が発生しないかもです。
|
2
|
+
`Create Table`は`list`に入れて`";".join()`するほうが問題が発生しないかもです。
|
3
|
-
sqlをprintして手で実行して確認してみてはどうでしょうか。
|
3
|
+
実行sqlを`print`して手で実行して確認してみてはどうでしょうか。
|
4
4
|
|
5
5
|
```Python
|
6
6
|
with closing(connect()) as conn:
|
1
追記
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Windows 10環境で試してみましたが、Cursor#executeの引数にmulti=Trueが必要でした。
|
1
|
+
Windows 10&mysql.connectorの環境で試してみましたが、Cursor#executeの引数にmulti=Trueが必要でした。
|
2
2
|
Create Tableはlistに入れて";".join()するほうが問題が発生しないかもです。
|
3
3
|
sqlをprintして手で実行して確認してみてはどうでしょうか。
|
4
4
|
|