给出csv格式的下表:
ID,X,Ref_ID
1,42.4242424242,99900000002
我使用以下脚本将此表导入mysql数据库:
from sqlalchemy import create_engine
import pandas as pd
df = pd.read_csv('MyFile.csv', sep=',')
engine = create_engine('mysql://usr:pass@localhost/mydatabase', echo=False)
df.to_sql("MyTable", engine, index=False)
结果:
ID: bigint(20)
X: double
Ref_ID : bigint(20)
有没有办法分析数据并使用适合数据的精确数据类型?目前,sqlalchemy似乎默认为整数列创建bigint(20),对包含小数值的列创建double;
应该是什么:
ID: int //no need to use a bigint, IDs are not that large
X: decimal(12,10) //This is a geographical coordinate represented by decimal(12,10)
Ref_ID: int(11)
注意:我的表中有很多列,因此无法为每个列单独手动指定所需的数据类型。
对于我的表,https://sqlizer.io确实产生了所需的结果,但我更喜欢使用python脚本而不是依赖于(慢)外部网站。