回答編集履歴

2

テーブル名について追記

2024/09/25 00:10

投稿

bsdfan
bsdfan

スコア4749

test CHANGED
@@ -18,3 +18,19 @@
18
18
  ```python
19
19
  df.to_sql(name=table_name, con=engine, if_exists='replace', index=False)
20
20
  ```
21
+
22
+ 追記
23
+ ----
24
+ pandas の `to_sql()` でテーブル名の指定をするときは、スキーマ名は含めないです。
25
+ ```python
26
+ table_name = 'DUMMY_F3'
27
+ df.to_sql(name=table_name, con=engine, if_exists='replace', index=False)
28
+ ```
29
+ スキーマを指定したい(デフォルトから変更したい)ときは、引数 `schema=` で指定します。
30
+ (今回のケースではschemaの指定はなくても大丈夫ではないでしょうか)
31
+ ```python
32
+ table_name = 'DUMMY_F3'
33
+ schema_name = 'R_TEST'
34
+ df.to_sql(name=table_name, con=engine, schema=schema_name, if_exists='replace', index=False)
35
+ ```
36
+

1

文言修正

2024/09/24 02:25

投稿

bsdfan
bsdfan

スコア4749

test CHANGED
@@ -1,4 +1,4 @@
1
- `with engine.connect()` はブロックを抜ける時にロールバックするので、明示的にコミットを入れる必要があると思います。
1
+ `with engine.connect()` 明示的にコミットをしないと、データベースに反映されないです。(ブロックを抜ける時にロールバックれる)
2
2
  https://docs.sqlalchemy.org/en/20/core/connections.html#commit-as-you-go
3
3
 
4
4
  ```python
@@ -7,14 +7,14 @@
7
7
  con.commit()
8
8
  ```
9
9
 
10
- `with engine.begin()` で書けば、ブロックを抜ける時にロールバックではなくコミットされるようになります。
10
+ `with engine.begin()` で書けば、ブロックを抜ける時にコミットされるようになります。
11
11
  https://docs.sqlalchemy.org/en/20/core/connections.html#connect-and-begin-once-from-the-engine
12
12
  ```python
13
13
  with engine.begin() as con:
14
14
  df.to_sql(name=table_name, con=con, if_exists='replace', index=False)
15
15
  ```
16
+
16
17
  トランザクションが不要なら、シンプルに `df.to_sql()` に `engine` を渡すのでもいいと思いました。
17
18
  ```python
18
19
  df.to_sql(name=table_name, con=engine, if_exists='replace', index=False)
19
20
  ```
20
-