回答編集履歴
3
charsetを追加
answer
CHANGED
@@ -28,10 +28,11 @@
|
|
28
28
|
|
29
29
|
def main() -> None:
|
30
30
|
config = {
|
31
|
+
'host': '127.0.0.1',
|
32
|
+
'database': 'XXXXXXX',
|
31
33
|
'user': 'XXXXXX',
|
32
34
|
'password': 'XXXXXX',
|
33
|
-
'host': '127.0.0.1',
|
34
|
-
'
|
35
|
+
'charset' : 'utf8'
|
35
36
|
}
|
36
37
|
with closing(connect(**config)) as conn:
|
37
38
|
with closing(conn.cursor()) as cursor:
|
2
追記
answer
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
|
8
8
|
```Python
|
9
9
|
sql = 'SELECT * FROM table1 WHERE FLG = 0 AND mail_ad = %s AND mail_date < %s'
|
10
|
-
cursor.execute(sql, (address, newdate))
|
10
|
+
cursor.execute(sql, (address, int(newdate)))
|
11
11
|
```
|
12
12
|
◇参考情報
|
13
13
|
[MySQL Connector/PythonからSQLを投げる](https://dev.classmethod.jp/server-side/python/query-with-mysql-connector-python/)
|
1
追記
answer
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
データベースがわからないので、`MySQL`と仮定して回答します。
|
2
2
|
質問文にはデータベース、使用ドライバを書いてくださいな。
|
3
3
|
|
4
|
-
|
5
4
|
> sql = "SELECT * FROM table1 WHERE FLG = 0 AND mail_ad = '%s'" % address " AND mail_date < '%d'" % newdate
|
6
5
|
|
7
6
|
1,`SQLインジェクション`を防止するために`SQL`を文字列連結するのではなく、プレースホルダを使用してくださいな。
|
@@ -10,13 +9,12 @@
|
|
10
9
|
sql = 'SELECT * FROM table1 WHERE FLG = 0 AND mail_ad = %s AND mail_date < %s'
|
11
10
|
cursor.execute(sql, (address, newdate))
|
12
11
|
```
|
12
|
+
◇参考情報
|
13
|
+
[MySQL Connector/PythonからSQLを投げる](https://dev.classmethod.jp/server-side/python/query-with-mysql-connector-python/)
|
13
14
|
|
14
15
|
2,`ORDER BY`句を指定しない時`SELECT`文は結果順序を保証しません。
|
15
16
|
`ORDER BY`はできるだけ付けてくださいな。
|
16
17
|
|
17
|
-
◇参考情報
|
18
|
-
[MySQL Connector/PythonからSQLを投げる](https://dev.classmethod.jp/server-side/python/query-with-mysql-connector-python/)
|
19
|
-
|
20
18
|
3,データベースやカーソルの`close`忘れを防ぐために[contextlib#closing](https://docs.python.jp/3/library/contextlib.html#contextlib.closing)を使用してくださいな。
|
21
19
|
|
22
20
|
データベース:MySQL
|
@@ -47,5 +45,4 @@
|
|
47
45
|
if __name__ == "__main__":
|
48
46
|
main()
|
49
47
|
|
50
|
-
|
51
48
|
```
|