回答編集履歴

2

edit

2017/11/30 09:50

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -24,15 +24,11 @@
24
24
 
25
25
  def update():
26
26
 
27
- with MySQLdb.connect(**params) as connector:
27
+ with MySQLdb.connect(**params) as cursor:
28
-
29
- cursor = connector.cursor()
30
28
 
31
29
  sql = "insert into test(id) value("+ n +")"
32
30
 
33
31
  cursor.execute(sql)
34
-
35
- connector.commit()
36
32
 
37
33
  n += 1
38
34
 
@@ -40,9 +36,7 @@
40
36
 
41
37
  def select():
42
38
 
43
- with MySQLdb.connect(**params) as connector:
39
+ with MySQLdb.connect(**params) as cursor:
44
-
45
- cursor = connector.cursor()
46
40
 
47
41
  sql = "select * from test"
48
42
 

1

add

2017/11/30 09:49

投稿

mkgrei
mkgrei

スコア8560

test CHANGED
@@ -10,11 +10,21 @@
10
10
 
11
11
 
12
12
 
13
+ params = {'host' : "localhost",
14
+
15
+ 'db' : "test",
16
+
17
+ 'user' : "root",
18
+
19
+ 'passwd' : "hoge",
20
+
21
+ 'charset' : "utf8"}
22
+
13
23
  n = 0
14
24
 
15
25
  def update():
16
26
 
17
- with MySQLdb.connect(host="localhost", db="test", user="root", passwd="hoge", charset="utf8") as connector:
27
+ with MySQLdb.connect(**params) as connector:
18
28
 
19
29
  cursor = connector.cursor()
20
30
 
@@ -30,7 +40,7 @@
30
40
 
31
41
  def select():
32
42
 
33
- with MySQLdb.connect(host="localhost", db="test", user="root", passwd="hoge", charset="utf8") as connector:
43
+ with MySQLdb.connect(**params) as connector:
34
44
 
35
45
  cursor = connector.cursor()
36
46
 
@@ -40,7 +50,7 @@
40
50
 
41
51
  result = cursor.fetchall()
42
52
 
43
- print (result)
53
+ print(result)
44
54
 
45
55
  ```
46
56