dtypeを指定すれば型指定出来そうということが分かりました。
https://jimaru.blog/programming/python/dataframe-to-sql/
https://jpdebug.com/p/1550433
https://docs.sqlalchemy.org/en/14/core/type_basics.html
を参考にして、それなりに型指定が出来ました。
python
1import pandas as pd
2from urllib.parse import quote_plus
3from sqlalchemy import create_engine
4from sqlalchemy.types import Float,Integer,String,Text,DateTime
5
6def alchemy_engine(db_path):
7 con_str = "DRIVER=" + \
8 "{Microsoft Access Driver (*.mdb, *.accdb)};" + \
9 f"DBQ={db_path};"
10 con_str = quote_plus(con_str)
11 engine = create_engine(
12 f"access+pyodbc:///?odbc_connect={con_str}",
13 echo=True)
14
15 return engine
16
17engine = alchemy_engine(r"D:\study\Python\test.accdb")
18
19df=pd.DataFrame({'A':[1,2,3],'B':[1.1,2.2,3],'C':['1','2','3'],'D':[None,None,None],'E':['1','2','3']})
20df.to_sql('testtable', engine, if_exists='replace', index=False,dtype={'A':Integer,'B':Float,'C':String(10),'D':DateTime,'E':Text})
21
間違っている箇所等あれば、ご指摘よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。