質問編集履歴

4

OSとDB Versionを追加

2024/08/01 10:56

投稿

python_heroku
python_heroku

スコア23

test CHANGED
File without changes
test CHANGED
@@ -10,11 +10,17 @@
10
10
  pandas:1.1.3
11
11
  SQLAlchemy:1.4.22
12
12
 
13
+ OS:Debian buster
14
+ Server DB:v17.3
15
+
13
16
  ### ケース1
14
17
  SQLAlchemyで作成したcreate_engineコネクションを利用したto_sqlによるDBへのデータ挿入処理においてエラーが発生する
15
18
  python:ver3.12.0
16
19
  pandas:2.2.2
17
20
  SQLAlchemy:1.4.22
21
+
22
+ OS:Debian buster
23
+ Server DB:v17.3
18
24
 
19
25
  ・警告内容
20
26
  ```ここに言語を入力
@@ -31,6 +37,9 @@
31
37
  python:ver3.12.0
32
38
  pandas:2.1.4
33
39
  SQLAlchemy:1.4.36
40
+
41
+ OS:Debian buster
42
+ Server DB:v17.3
34
43
 
35
44
  ・警告内容
36
45
  ```

3

with句の指摘と誤記について

2024/08/01 10:29

投稿

python_heroku
python_heroku

スコア23

test CHANGED
File without changes
test CHANGED
@@ -115,7 +115,7 @@
115
115
 
116
116
  ```python
117
117
  engine = create_engine(
118
- "mssql*pyodbc://{user_id}:{password}@{localhost}:{port}/{dbname}?driver={driver}".format(
118
+ "mssql+pyodbc://{user_id}:{password}@{localhost}:{port}/{dbname}?driver={driver}".format(
119
119
  localhost = "略",
120
120
  port = "略",
121
121
  dbname = "略",
@@ -125,4 +125,18 @@
125
125
  )
126
126
  ```
127
127
 
128
+ with句のインデントの指摘を頂いて、確かになと思ったので、下記も試してみたのですが、同様のエラーが生じました
129
+ ```python
130
+ with engine.begin() as conn:
131
+ try:
132
+ df.to_sql(tablename, engine, if_exists="append")
128
133
 
134
+ conn.commit()
135
+
136
+ except Exception as ex:
137
+ conn.rollback()
138
+ ```
139
+ ```
140
+ 'Connection' object has no attribute 'commit'
141
+ ```
142
+

2

, if_exists="append" の記述をto_sqlに追加

2024/08/01 06:23

投稿

python_heroku
python_heroku

スコア23

test CHANGED
File without changes
test CHANGED
@@ -53,7 +53,7 @@
53
53
 
54
54
  try:
55
55
  # 下記のコネクション微妙に違うかも。。
56
- df.to_sql(tablename, engine.raw_connection().engine)
56
+ df.to_sql(tablename, engine.raw_connection().engine, if_exists="append")
57
57
 
58
58
  except Exception as ex:
59
59
  engine.raw_connection().rollback()
@@ -84,7 +84,7 @@
84
84
 
85
85
  try:
86
86
  with engine.begin() as conn:
87
- df.to_sql(tablename, engine)
87
+ df.to_sql(tablename, engine, if_exists="append")
88
88
 
89
89
  conn.commit()
90
90
 

1

create_engine作成処理について追記

2024/08/01 06:20

投稿

python_heroku
python_heroku

スコア23

test CHANGED
File without changes
test CHANGED
@@ -86,7 +86,7 @@
86
86
  with engine.begin() as conn:
87
87
  df.to_sql(tablename, engine)
88
88
 
89
- conn.commit()
89
+ conn.commit()
90
90
 
91
91
  except Exception as ex:
92
92
  conn.rollback()
@@ -110,4 +110,19 @@
110
110
  情報に不足あればご指摘下さい
111
111
 
112
112
 
113
+ ### 頂いた回答に対しての追記
114
+ create_engineの方法間違っている?と思いまして、該当部分の記述追加します。元々のversionではうまくいったので、これが現在のバージョンだとダメなのか?などが頂いた情報を参考に試してもみたのですが、引き続き同事象が継続していました。
113
115
 
116
+ ```python
117
+ engine = create_engine(
118
+ "mssql*pyodbc://{user_id}:{password}@{localhost}:{port}/{dbname}?driver={driver}".format(
119
+ localhost = "略",
120
+ port = "略",
121
+ dbname = "略",
122
+ userid = "略",
123
+ password = "略",
124
+ driver = "ODBC Driver 17 for SQL Server",
125
+ )
126
+ ```
127
+
128
+