安装

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. 剑指offer——面试题11:快速排序

    #include"iostream" #include"random" using namespace std; /* void Swap(int &a ...

  2. switch case 注意事项+1 及 case合并综合练习例子

    case可以合并: 练习11:根据输入的星期,得到具体每天做的事情.星期一学习,星期二学习,星期三自习,星期四学习,星期五自习,星期六学习,星期日学习 class Switch02{ public s ...

  3. DB Intro - MongoDB Relations

    https://www.quackit.com/mongodb/tutorial/mongodb_create_a_relationship.cfm

  4. 使用SeaJS实现模块化JavaScript开发【转】

    前言 SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制.与jQuery等JavaScript框架不同,SeaJS不会扩展封 ...

  5. JDBC的PreparedStatement启动事务使用批处理executeBatch()

    JDBC使用MySQL处理大数据的时候,自然而然的想到要使用批处理, 普通的执行过程是:每处理一条数据,就访问一次数据库: 而批处理是:累积到一定数量,再一次性提交到数据库,减少了与数据库的交互次数, ...

  6. FileZilla的使用(包括Server和Client两个程序)

    1.安装 FileZillaServer和FileZillaClient,到官网去下载 https://filezilla-project.org/ 2.启动 FileZillaServer 它会提示 ...

  7. java中HashMap的keySet()和values()

    我们通常说,keySet()返回所有的键,values()返回所有的值,其实是不太对的,因为无论是keySet()和values(),其实都没有实质的内容,且容我慢慢说来. 他们前者返回了一个Set, ...

  8. centos6.5 源码编译 mysql5.6.21

    1.yum安装各个依赖包 [root@WebServer ~]# yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel autoconf* automa ...

  9. [转]Load ASP.NET MVC Partial Views Dynamically Using jQuery

    本文转自:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx Most of the t ...

  10. mysql 乱码问题的捣鼓

    mysql在ubuntu的终端下出现中文乱码的问题: 先学着在不改数据库的情况下对my.cnf配置文件进行修改, 主要的是设置 default-character-set=utf8 但是设置完后数据库 ...