python - python - 到 SQL Server 的数据框
问题描述
请不要使用 sqlalchemy,因为我没有端口信息 - 我只有服务器名称、数据库以及用户名和密码。
import numpy as np
import pandas as pd
df = pd.DataFrame({'num_legs': [2, 4, 8, 0],
'num_wings': [2, 0, 0, 0],
'num_specimen_seen': [8, 2, 5, 6]},
)
print(df)
我在 SQL Server 中有一个表,Animal
名为Legs
, wings
, SpecSeen
;
CREATE TABLE Habitat
(
Legs int,
wings int,
SpecSeen nvarchar(50)
)
ServerName: SQL15A
Database: Habitat
SQL username: QATuser
Password: ****
我需要将此 DataFrame 插入到 SQL Server 表中,也不需要将 DataFrame 索引列插入到数据库表中。
解决方案
import sqlalchemy
import pyodbc
engine = sqlalchemy.create_engine("mssql+pyodbc://<username>:<password>@<dsnname>")
# write the DataFrame to a table in the sql database
df.to_sql("table_name", engine)
推荐阅读
- css - Bootstrap 4: Why isn't this aligned to the bottom?
- autodesk-viewer - Safari returns lmvworker 404 error when loading viewer
- android - Android 应用在收到新的 FCM 消息时崩溃(前台和后台)
- android - Android 数据绑定:@Binable vs Observablefield 性能
- java - 如何永久删除列表视图项?
- python - 在 statsmodels OLS 类中使用分类变量
- excel - 使用 Excel 识别因子的别名
- java - 如何在棋盘上放置圆圈?
- babeljs - @babel/parser 是否有“容错”或“松散”模式?
- php - 为什么我在使用 restful 的 ionic 应用程序中得到 HttpErrorResponse?