teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

Python 3.6.5 SQLite 3.28.0 select * from twitter_users;でシングルクオートが消えているのを確認しました。

2019/10/29 05:42

投稿

kalon
kalon

スコア198

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,6 @@
1
1
  Python 3.6.5
2
2
  SQLite 3.28.0
3
+ select * from twitter_users;でシングルクオートが消えているのを確認しました。
3
4
 
4
5
  以下のスクリプトを書きました
5
6
  ```Python

2

動作環境の追加

2019/10/29 05:42

投稿

kalon
kalon

スコア198

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,6 @@
1
+ Python 3.6.5
2
+ SQLite 3.28.0
3
+
1
4
  以下のスクリプトを書きました
2
5
  ```Python
3
6
  import sqlite3

1

新たな試みを試したがダメだった

2019/10/29 00:29

投稿

kalon
kalon

スコア198

title CHANGED
File without changes
body CHANGED
@@ -29,6 +29,38 @@
29
29
  db.c.execute("update twitter_users set comment = '" + comment + 'where name = "' + name + '"')
30
30
  sqlite3.OperationalError: near "s": syntax error
31
31
 
32
+ こちらでもダメでした。
33
+ ```python
34
+ import sqlite3
35
+
36
+
37
+ class Database:
38
+ def __init__(self):
39
+ self.dbpath = 'db.sqlite3'
40
+ self.conn = sqlite3.connect(self.dbpath)
41
+ self.c = self.conn.cursor()
42
+
43
+
44
+ db = Database()
45
+
46
+
47
+ def update_comment(name, comment='null'):
48
+ db.c.execute('update twitter_users set comment = %s where name = %s' % (name, comment))
49
+ db.conn.commit()
50
+
51
+
52
+ update_comment('Jikkenndesu', "How's day?")
53
+ ```
54
+
55
+ エラー:
56
+ Traceback (most recent call last):
57
+ File "test.py", line 19, in <module>
58
+ update_comment('Jikkenndesu', "How's day?")
59
+ File "test.py", line 15, in update_comment
60
+ db.c.execute('update twitter_users set comment = %s where name = %s' % (name, comment))
61
+ sqlite3.OperationalError: unrecognized token: "'s day?"
62
+
63
+
32
64
  ユーザがコメントに「'(シングルクオート)」を入力してきてもエラーにならないようにしたいのです。
33
65
 
34
66
  アドバイスとご教授のほどよろしくお願いします。