Python连接SqlServer数据库-pymssql进阶

本文介绍了如何在Windows环境下安装pymssql,提供了详细的安装步骤和使用示例。通过导入pymssql模块,创建连接对象,设置参数如主机名、用户名、密码、数据库名称等,实现Python与SqlServer的连接。此外,文章还涵盖了连接对象的管理,如设置自动提交、提交和回滚事务,以及游标对象的操作,包括调用存储过程和执行SQL语句。
摘要由CSDN通过智能技术生成

  • 支持环境:

Python: Python 2.x: 2.7 or newer. Python 3.x: 3.3 or newer.
FreeTDS: 0.82 or newer.
Cython: 0.15 or newer.
Microsoft SQL Server:    2005 or newer.
   

  • Windows安装方式:

从https://www.lfd.uci.edu/~gohlke/pythonlibs/下载最新的Pymssq的whl,

pymssql‑2.1.4.dev5‑cp37‑cp37m‑win32.whl

pymssql‑2.1.4.dev5‑cp37‑cp37m‑win_amd64.whl

下载完成后通过pip install pymssql‑2.1.4.dev5‑cp37‑cp37m‑win_amd64.whl 安装

使用方式

A、使用pymssql     

1、导入包

import pymssql     

1、创建连接对象:

conn=pymssql.connect(server='.'user=''password=''database=''timeout=0login_timeout=60charset='UTF-8'as_dict=Falsehost=''appname=Noneport='1433'conn_propertiesautocommit=Falsetds_version='7.1')

Parameters: 参数说明
server (str) – database host 主机名(主机地址)
user (str) – database user to connect as 用户名
password (str) – user’s password 密码
database (str) – the database to initially connect to 数据库名称

timeout (int) – query timeout in seconds, default 0 (no timeout) 查询超时
login_timeout (int) – timeout for connection and login in seconds, default 60 登录超时
charset (str) – character set with which to connect to the database 连接所使用字符集,中文一般使用 'utf8'

conn_properties – SQL queries to send to the server upon connection establishment. Can be a string or another kind of iterable of strings. Default value: See _mssql.connect() 连接数据库自动发起的查询语句,默认为‘

SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET ANSI_NULLS ON;
SET ANSI_NULL_DFLT_ON ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ANSI_NULL_DFLT_ON ON;
SET CURSOR_CLOSE_ON_COMMIT ON;
SET QUOTED_IDENTIFIER ON;
SET TEXTSIZE 2147483647;
as_dict (bool) – whether rows should be returned as dictionaries instead of tuples是否使用字典作为查询结果返回方式,默认为元组
appname (str) – Set the application name to use for the connection
port (str) – the TCP port to use to connect to the server  TCP 端口
autocommit (bool) – Whether to use default autocommiting mode or not 连接关闭时是否自动提交,默认为不自动提交
tds_version (str) – TDS protocol version to use. 使用的TDS版本,默认为7.1

*设置最大连接数 

pymssql. set_max_connections ( number )

Sets maximum number of simultaneous database connections allowed to be open at any given time. Default is 25.

2、连接对象相关操作

Connection.autocommit(status)设置自动提交,status为Bool值

Connection.close()关闭连接
Connection.cursor()返回一个cursor对象,用于数据库查询等操作

Connectreturnvalueion.commit()提交更改

Connection.rollback()回滚更改

3、cursor对象相关操作

属性

  • 4
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用Python连接SQL Server数据库的步骤: 1. 安装pyodbc模块 在Python连接SQL Server需要使用模块pyodbc,因此需要先安装该模块,使用命令`pip install pyodbc`。 2. 导入pyodbc模块 安装完成后,在Python中导入pyodbc模块,使用`import pyodbc`。 3. 连接SQL Server数据库 使用pyodbc的`connect`方法连接SQL Server数据库。需要提供数据库连接字符串,格式为: ``` DRIVER={SQL Server};SERVER=server_name;DATABASE=database_name;UID=username;PWD=password ``` 其中,server_name、database_name、username、password需要根据实际情况进行替换。 示例代码: ```python import pyodbc # 连接数据库 conn_str = 'DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=sa;PWD=password' conn = pyodbc.connect(conn_str) # 创建游标对象 cursor = conn.cursor() # 查询数据 cursor.execute('SELECT * FROM user') # 打印查询结果 for row in cursor: print(row) # 关闭游标和数据库连接 cursor.close() conn.close() ``` 以上代码连接了本地服务器上的testdb数据库,并查询了其中一个叫做user的表的所有数据。每一行数据是一个元组,其中包含各个字段的值。 注意事项: - 在连接字符串中,需要将数据库名、用户名、密码等敏感信息替换成实际的内容; - 如果SQL Server数据库使用了Windows身份验证,可以省略`UID`和`PWD`两个参数,将其设置为空字符串,然后在连接字符串中添加`Trusted_Connection=yes`。 参考文档: - [pyodbc](https://github.com/mkleehammer/pyodbc/wiki)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值