当前位置:网站首页>利用pandas读取SQL Sever数据表
利用pandas读取SQL Sever数据表
2022-06-24 09:40:00 【Cpsu】
python的pandas库读取SQL sever有两种方法。一种使用pymssql,另一种使用sqlalchemy。这里只是将数据库中的表读取为DataFrame,不进行修改等表操作。
目录
一、pymssql
1.安装
可以直接用以下命令安装pymssql。
pip install pymssql
pip install sqlalchemy
2.连接数据库
这里也分为两种情况,一种是直接使用windows验证登陆,另一种是进行SQL Sever身份验证即账号密码登陆。这里可以进行设置,可以同时使用两种登录方式。
1.登录设置
如果要设置两种方式同时登陆,我们首先用windows验证登录以后
点击安全性,选择如图所示的选项以后点击确定,关闭该窗口。
然后点击安全性,在登录名中右键点击sa,再点击菜单栏的最下方的属性。
首先点击常规,设置密码,然后再点击状态,启用登陆。点击确定关闭。

最后我们需要重启SQL Sever。在开始菜单栏找到SQL Sever配置管理器。找到SQL Sever网络配置。在MSSQL Sever协议中启用“Named Pipes”和“TCP/IP”。然后在SQL Sever服务中,右键点击SQL Sever(MSSQL Sever),然后点击重新启动。这样就设置完成了。


3.获取数据库表
import pymssql
import pandas as pd
""" 如果使用sql sever身份验证的话用下面的代码 conn = pymssql.connect(host, user, password, "连接默认数据库名称") """
#这里直接用windows验证登陆,无需账号密码,database为需要连接的数据库名称
conn = pymssql.connect(host="",database="db_databsae", charset="utf8")
#使用pandas库的read_sql方法,输入select语句即可
df = pd.read_sql("select * from score",con=conn)
print(df)
conn.close()
这里的host参数是服务器名称,我们一般连接到本地服务器为localhost,就是ip地址加端口号。可以按照如下方式找到。右键ip协议然后点击属性,这里host即为 ip地址:端口号 。比如 127.0.0.1:1533 。127.0.0.1为ip,1533为端口号。这样我们就可以成功获取到数据了。
二、sqlalchemy
有了上一部分的设置,这里直接上代码,都是类似的。
import sqlalchemy as sqla
#windows验证登陆
#这里只需把host换成自己的host和databsae换成自己的数据库名称
db = sqla.create_engine("mssql+pymssql://@host/databsae?charset=utf8")
pd.read_sql("select * from score;",db)
#sql sever身份验证登陆
#同样换掉host和数据库名称,同时换掉sa登陆的密码,这里为123
db = sqla.create_engine("mssql+pymssql://sa:[email protected]/db_databsae?charset=utf8")
pd.read_sql("select * from test;",db)
边栏推荐
- canvas管道动画js特效
- TP5 using post to receive array data times variable type error: solution to array error
- 416-二叉树(前中后序遍历—迭代法)
- 有关二叉树 的基本操作
- GIS实战应用案例100篇(十四)-ArcGIS属性连接和使用Excel的问题
- Analysis of 43 cases of MATLAB neural network: Chapter 32 time series prediction of wavelet neural network - short-term traffic flow prediction
- Oracle viewing data file header SCN information
- Latex formula and table recognition
- 桌面软件开发框架大赏
- 413 binary tree Foundation
猜你喜欢

Phpstrom code formatting settings

Servlet fast foundation building

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

GeoGebra 实例 时钟

indexedDB本地存储,首页优化

Canvas draw picture

简单的价格表样式代码

编程题(持续更新)

411 stack and queue (20. valid parentheses, 1047. delete all adjacent duplicates in the string, 150. inverse Polish expression evaluation, 239. sliding window maximum, 347. the first k high-frequency

5 minutes, excellent customer service chat handling skills
随机推荐
Regular matching mailbox
Oracle database expdp only exports tables
桌面软件开发框架大赏
How do novices choose the grade of investment and financial products?
微信小程序學習之 實現列錶渲染和條件渲染.
请问有国内靠谱低手续费的期货开户渠道吗?网上开户安全吗?
Recursive traversal of 414 binary tree
Which of the top ten securities companies has the lowest Commission and is the safest and most reliable? Do you know anything
Practical analysis: implementation principle of APP scanning code landing (app+ detailed logic on the web side) with source code
How to manage massive network infrastructure?
涂鸦智能携多款重磅智能照明解决方案,亮相2022美国国际照明展
el-table表格的拖拽 sortablejs
js单例模式
LeetCode: 377. Combined sum IV
YOLOv6:又快又准的目标检测框架开源啦
如何规范化数据中心基础设施管理流程
MYSQL数据高级
411-栈和队列(20. 有效的括号、1047. 删除字符串中的所有相邻重复项、150. 逆波兰表达式求值、239. 滑动窗口最大值、347. 前 K 个高频元素)
Geogebra instance clock
canvas掉落的小球重力js特效动画