sql-server - Pandas dataframe with less number of columns to SQL Server Table
问题描述
I am trying to insert some data into a table in sql-server using sqlalchemy and the function df.to_sql
.
The problem is that the dataframe doesn't have the same number of columns (it has less columns than the sql server table) as the table and for the columns that the dataframe doesn't have, it inserts NULL values. Is there any way to replace the nulls with blank spaces.
engine = sqlalchemy.create_engine("mssql+pyodbc://"+creds.un+":"+creds.pw+"@"+creds.svr+"/"+creds.db+"?driver=ODBC+Driver+17+for+SQL+Server")
final_df.to_sql("table", con = engine, if_exists = "append", index = False)
final_df is missing about 2 columns that are not nullable.
解决方案
您可以将数据库列设置为默认值 '' 而不是 null 省略值,或者在数据框中指定列,也许做一个 final_df.fillna('', inplace=True)
推荐阅读
- regex - 带变量的 AWK 负正则表达式
- python - 错误:ModuleNotFoundError:没有名为“pyqtgraph”的模块?
- python - Tensorflow - 无法获得损失和输入图像之间的梯度
- c++ - '......' 中的成员 '.......' 的错误请求,它是非类类型 '.....'
- ruby-on-rails - 你在 api-only rails 应用程序中设置 root :to 是什么?
- python - Python 打印/输出组
- reactjs - React应用程序,从初始获取请求获取数据后,onChange不起作用
- angular - 从以前的组件 Angular 中选择列表中的对象
- java - 使用 PreparedStatement 插入 MySQL 数据库时出现 java.sql.SQLSyntaxErrorException
- ios - iOS Swift - didDiscover 外围设备未在后台调用