python入门22 pymssql模块(python连接sql server查询)
安装
pip install pymssql
连接数据库 pymssql.connect()
# coding:utf-8 import pymssql server = '192.168.8.1' #服务器ip或名称
user = 'sa' #用户名
password = '' #密码
database = 'test' #数据库 dbconnect = pymssql.connect(server = server,user = user,password=password,database = database) #连接到数据库
操作数据库 connect.cursor() ,
fetchone() 取一行
fetchall() 取所有结果
dbcursor = dbconnect.cursor() #游标
dbcursor.execute('select top 2 name,major from test_student')
#一行一行的取数
row = dbcursor.fetchone()
print(row)
row = dbcursor.fetchone()
print(row)
row = dbcursor.fetchone() #取完数据,None
print(row)

#取出所有查询结果
rows = dbcursor.fetchall()
print(rows) # list
rows = dbcursor.fetchall() #再取空
print(rows) # []

游标加上 as_dict=True。结果显示dict
dbcursor = dbconnect.cursor() #游标
dbcursor.execute('select top 10 name,major from test_student')
print(dbcursor.fetchall())

dbcursor = dbconnect.cursor(as_dict=True) #游标
dbcursor.execute('select top 10 name,major from test_student')
print(dbcursor.fetchall())

最后记得关闭游标和连接
dbcursor.close()
dbconnect.close()
官网: http://pymssql.org/en/stable/
python入门22 pymssql模块(python连接sql server查询)的更多相关文章
- Python基于Pymssql模块实现连接SQL Server数据库的方法
首先,安装pymssql第三方库pip install pymssql 其次,导入pymssql库 最后们就可以连接数据库了 import pymssql server = "10.10.9 ...
- python入门23 pymssql模块(python连接sql server增删改数据 )
增删改数据必须connect.commit()才会生效 回滚函数 connect.rollback() 连接数据库 ''' dinghanhua sql server增删改 ''' import py ...
- python 使用pymssql连接sql server数据库
python 使用pymssql连接sql server数据库 #coding=utf-8 #!/usr/bin/env python#------------------------------ ...
- python 连接sql server
linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...
- python连接sql server数据库实现增删改查
简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...
- python 连接 SQL Server 数据库
#!/usr/bin/python # -*- coding:utf-8 -*- import pymssql import pyodbc host = '127.0.0.1:1433' user = ...
- Python web(Django)连接Sql server
(开开心心每一天~ ---虫瘾师) Python Web(Django) 与SQL SERVRE的连接----Come QQ群:607021567(里面有很多开源代码和资料,并且python的游戏也有 ...
- Python入门之第三方模块安装
Python入门之第三方模块安装 平台:Win10 x64 + Anaconda3-5.3.0 (+Python3.7.0) Issue说明:pip install line_profiler-2.1 ...
- Python 学习 第17篇:从SQL Server数据库读写数据
在Python语言中,从SQL Server数据库读写数据,通常情况下,都是使用sqlalchemy 包和 pymssql 包的组合,这是因为大多数数据处理程序都需要用到DataFrame对象,它内置 ...
随机推荐
- 实现Java程序跨平台运行十二个注意事项
[转自] http://blog.chinaunix.net/uid-20550186-id-1927257.html 使用Java语言编写应用程序最大的优点在于"一次编译,处处运行&quo ...
- 学习python-跨平台获取键盘事件
class _Getch: """Gets a single character from standard input. Does not echo to the sc ...
- 剪邮票--蓝桥杯--dfs--思路超清晰
剪邮票 如[图1.jpg], 有12张连在一起的12生肖的邮票. 现在你要从中剪下5张来,要求必须是连着的. (仅仅连接一个角不算相连) 比如,[图2.jpg],[图3.jpg]中,粉红色所示部分就是 ...
- win10 安装MySQL8.0.11记录。
参考了博客A:https://blog.csdn.net/m0_37788308/article/details/79965378 博客B:https://blog.csdn.net/fxbin123 ...
- centos7安装SourceCodePro字体
1. 下载SourceCodePro字体,后缀应为.ttf. 2. 将字体文件复制到fonts(/usr/share/fonts)文件夹下: [root@centos fonts]# mv /home ...
- Android中的CardView使用
Android 5.0 版本中新增了CardView,CardView继承自FrameLayout类,并且可以设置圆角和阴影,使得控件具有立体性,也可以包含其他的布局容器和控件. 1.配置build. ...
- ul+js模拟select
html css .select_box{ float: left; } .select_box input{ width: 160px; height: 30px; text-align: ce ...
- .net使用redis入门笔记
1.学习blog:http://www.cnblogs.com/yangecnu/p/Introduct-Redis-in-DotNET.html 2.redis官网:http://redis.io/ ...
- 2017年11月3日 VS三大类&数组&VS的冒泡排序&集合&泛型集合
三大类 共分为两个大类: 基本数据型&引用类型 基本数据型---值类型---整型---常用的整型: Int , 长整型: Long, 小整型: byle, 中整型 short --浮点型 - ...
- DJango小总结一
views.py def func(request): # 包含所有的请求数据 ... ...