pandas to_sql all columns as nvarchar

后端 未结 2 1086
庸人自扰
庸人自扰 2020-12-07 22:06

I have a pandas dataframe that is dynamically created with columns names that vary. I\'m trying to push them to sql, but don\'t want them to go to mssqlserver as the defaul

2条回答
  •  盖世英雄少女心
    2020-12-07 22:44

    You can create this dict dynamically if you do not know the column names in advance:

    from sqlalchemy.types import NVARCHAR
    df.to_sql(...., dtype={col_name: NVARCHAR for col_name in df})
    

    Note that you have to pass the sqlalchemy type object itself (or an instance to specify parameters like NVARCHAR(length=10)) and not a string as in your example.

提交回复
热议问题