回答編集履歴
1
実際の使用例を追記
test
CHANGED
@@ -1,6 +1,32 @@
|
|
1
1
|
dtypeを指定すれば型指定出来そうということが分かりました。
|
2
|
+
https://jimaru.blog/programming/python/dataframe-to-sql/
|
2
3
|
https://jpdebug.com/p/1550433
|
3
|
-
https://docs.sqlalchemy.org/en/1
|
4
|
+
https://docs.sqlalchemy.org/en/14/core/type_basics.html
|
4
|
-
を参考に
|
5
|
+
を参考にして、それなりに型指定が出来ました。
|
5
6
|
|
7
|
+
```python
|
8
|
+
import pandas as pd
|
9
|
+
from urllib.parse import quote_plus
|
10
|
+
from sqlalchemy import create_engine
|
11
|
+
from sqlalchemy.types import Float,Integer,String,Text,DateTime
|
12
|
+
|
13
|
+
def alchemy_engine(db_path):
|
14
|
+
con_str = "DRIVER=" + \
|
15
|
+
"{Microsoft Access Driver (*.mdb, *.accdb)};" + \
|
16
|
+
f"DBQ={db_path};"
|
17
|
+
con_str = quote_plus(con_str)
|
18
|
+
engine = create_engine(
|
19
|
+
f"access+pyodbc:///?odbc_connect={con_str}",
|
20
|
+
echo=True)
|
21
|
+
|
22
|
+
return engine
|
23
|
+
|
24
|
+
engine = alchemy_engine(r"D:\study\Python\test.accdb")
|
25
|
+
|
26
|
+
df=pd.DataFrame({'A':[1,2,3],'B':[1.1,2.2,3],'C':['1','2','3'],'D':[None,None,None],'E':['1','2','3']})
|
27
|
+
df.to_sql('testtable', engine, if_exists='replace', index=False,dtype={'A':Integer,'B':Float,'C':String(10),'D':DateTime,'E':Text})
|
28
|
+
|
29
|
+
```
|
30
|
+
|
6
|
-
|
31
|
+
間違っている箇所等あれば、ご指摘よろしくお願いします。
|
32
|
+
|