安装

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查询)的更多相关文章

  1. Python基于Pymssql模块实现连接SQL Server数据库的方法

    首先,安装pymssql第三方库pip install pymssql 其次,导入pymssql库 最后们就可以连接数据库了 import pymssql server = "10.10.9 ...

  2. python入门23 pymssql模块(python连接sql server增删改数据 )

    增删改数据必须connect.commit()才会生效 回滚函数 connect.rollback() 连接数据库 ''' dinghanhua sql server增删改 ''' import py ...

  3. python 使用pymssql连接sql server数据库

    python 使用pymssql连接sql server数据库   #coding=utf-8 #!/usr/bin/env python#------------------------------ ...

  4. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

  5. python连接sql server数据库实现增删改查

    简述 python连接微软的sql server数据库用的第三方模块叫做pymssql(document:http://www.pymssql.org/en/stable/index.html).在官 ...

  6. python 连接 SQL Server 数据库

    #!/usr/bin/python # -*- coding:utf-8 -*- import pymssql import pyodbc host = '127.0.0.1:1433' user = ...

  7. Python web(Django)连接Sql server

    (开开心心每一天~ ---虫瘾师) Python Web(Django) 与SQL SERVRE的连接----Come QQ群:607021567(里面有很多开源代码和资料,并且python的游戏也有 ...

  8. Python入门之第三方模块安装

    Python入门之第三方模块安装 平台:Win10 x64 + Anaconda3-5.3.0 (+Python3.7.0) Issue说明:pip install line_profiler-2.1 ...

  9. Python 学习 第17篇:从SQL Server数据库读写数据

    在Python语言中,从SQL Server数据库读写数据,通常情况下,都是使用sqlalchemy 包和 pymssql 包的组合,这是因为大多数数据处理程序都需要用到DataFrame对象,它内置 ...

随机推荐

  1. PIE SDK矢量数据编辑的撤销和回退

    1.功能简介 在数据的编辑过程中难免会出现失误,撤销和回退可以更好的编辑,下面对矢量数据编辑的撤销和回退功能进行介绍. 2.功能实现说明 2.1. 实现思路及原理说明 第一步 调用UndoComman ...

  2. PIE SDK分类统计

    1. 算法功能简介 分类统计功能是将分类后的结果统计输出. PIE SDK支持算法功能的执行,下面对分类统计算法功能进行介绍. 2. 算法功能实现说明 2.1. 实现步骤 第一步 算法参数设置 第二步 ...

  3. Merge Sorted Array II

    Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B= ...

  4. MySQL主从复制,以及双机热备

    MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展.多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能. 下图就描述了一个多个数 ...

  5. 安装Newton版Glance

    Image Service 本文介绍在controller节点上安装.配置Image服务 glance,镜像存储在本地文件系统 安装准备 controller 节点 ip:192.168.81.11 ...

  6. LeetCode 112.路径总和(C++)

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22 ...

  7. ClouderManger搭建大数据集群时ERROR 2003 (HY000): Can't connect to MySQL server on 'ubuntucmbigdata1' (111)的问题解决(图文详解)

    问题详情 相关问题的场景,是在我下面的这篇博客里 Cloudera Manager安装之利用parcels方式(在线或离线)安装3或4节点集群(包含最新稳定版本或指定版本的安装)(添加服务)(Ubun ...

  8. C# 获取字符串长度

    int leng = System.Text.Encoding.Default.GetBytes(attachfileId2).Length;

  9. PHP常用的一些数组操作总结

    1.array_values() :返回包含数组中所有键值的数组,不保留键名. 2.array_diff() 函数返回两个数组的差集数组.该数组包括了所有在被比较的数组中,但是不在任何其他参数数组中的 ...

  10. 对 Vue 的理解(一)

    一.什么是 Vue ? 首先,Vue 是一个 MVVM 框架,M -- 模型,就是用来定义驱动的数据,V -- 视图,是经过数据改变后的 html,VM -- 框架视图,就是用来实现双向绑定的中间桥梁 ...