回答編集履歴

3

追記

2019/10/29 05:11

投稿

quickquip
quickquip

スコア11040

test CHANGED
@@ -92,7 +92,7 @@
92
92
 
93
93
  ```
94
94
 
95
- だとnameとcommentが逆ですからwhere句でヒットしないので0件のupdateで終了する)
95
+ だとnameとcommentが逆ですからwhere句でヒットしないので0件のupdateで終了しま。updateす前の情報を見て勘違いしているだけではないでしょうか
96
96
 
97
97
 
98
98
 

2

追記

2019/10/29 05:11

投稿

quickquip
quickquip

スコア11040

test CHANGED
@@ -75,3 +75,39 @@
75
75
  パッとやる限りは再現しないですね……。
76
76
 
77
77
  DBを中身をどうやって確認したかと、Pythonのバージョンを明記してもらえるといいかと思いました。
78
+
79
+
80
+
81
+ ----
82
+
83
+
84
+
85
+ 追記
86
+
87
+
88
+
89
+ ```python
90
+
91
+ db.c.execute('update twitter_users set comment = ? where name = ?', (name, comment))
92
+
93
+ ```
94
+
95
+ だとnameとcommentが逆です(からwhere句でヒットしないので0件のupdateで終了する)。
96
+
97
+
98
+
99
+ ```python
100
+
101
+ db.c.execute('update twitter_users set comment = ? where name = ?', (comment, name))
102
+
103
+ ```
104
+
105
+
106
+
107
+ ```python
108
+
109
+ db.c.execute('update twitter_users set comment = :comment where name = :name', {'name': name, 'comment': comment})
110
+
111
+ ```
112
+
113
+ にしましょう。

1

追記

2019/10/29 00:26

投稿

quickquip
quickquip

スコア11040

test CHANGED
@@ -7,3 +7,71 @@
7
7
  > たいてい、SQL 操作では Python 変数の値を使う必要があります。この時、クエリーを Python の文字列操作を使って構築することは安全とは言えないので、すべきではありません。そのようなことをするとプログラムが SQL インジェクション攻撃に対し脆弱になります (https://xkcd.com/327/ ではどうなってしまうかをユーモラスに描いています)。
8
8
 
9
9
  代わりに、DB-API のパラメータ割り当てを使います。 ? を変数の値を使いたいところに埋めておきます。その上で、値のタプルをカーソルの execute() メソッドの第2引数として引き渡します。(他のデータベースモジュールでは変数の場所を示すのに %s や :1 などの異なった表記を用いることがあります。) 例を示します:
10
+
11
+
12
+
13
+ ----
14
+
15
+
16
+
17
+ コメントへ
18
+
19
+
20
+
21
+ ```python
22
+
23
+ import sqlite3
24
+
25
+
26
+
27
+ conn = sqlite3.connect('example.db')
28
+
29
+ c = conn.cursor()
30
+
31
+
32
+
33
+ # Create table
34
+
35
+ c.execute('''CREATE TABLE stocks (date text, trans text, symbol text, qty real, price real)''')
36
+
37
+
38
+
39
+ c.execute("INSERT INTO stocks VALUES ('2006-01-05', ? ,'RHAT',100,35.14)", ("ho'ge",))
40
+
41
+
42
+
43
+ # Save (commit) the changes
44
+
45
+ conn.commit()
46
+
47
+
48
+
49
+ for row in c.execute('SELECT * FROM stocks ORDER BY price'):
50
+
51
+ print(row)
52
+
53
+
54
+
55
+ # We can also close the connection if we are done with it.
56
+
57
+ # Just be sure any changes have been committed or they will be lost.
58
+
59
+ conn.close()
60
+
61
+ ```
62
+
63
+
64
+
65
+ 結果
66
+
67
+ ```
68
+
69
+ ('2006-01-05', "ho'ge", 'RHAT', 100.0, 35.14)
70
+
71
+ ```
72
+
73
+
74
+
75
+ パッとやる限りは再現しないですね……。
76
+
77
+ DBを中身をどうやって確認したかと、Pythonのバージョンを明記してもらえるといいかと思いました。