pymssql 安装和使用

本文档详细介绍了在Windows上安装Python 2.7、Eclipse 4.3、pydev 3.9及pymssql 2.01的步骤,并提供了测试连接SQL Server的代码示例。在安装过程中,强调了64位和32位软件的对应关系,以及pydev对Java版本的要求。完成安装后,通过示例代码展示了如何使用pymssql连接数据库并执行查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows环境

安装环境:python 2.7 + Eclipse 4.3 + pydev 3.9 + pymssql 2.01
注意:
64位Eclipse必须使用64位JDK,32位Eclipse必须使用32位JDK



1. 下载 pydev
http://sourceforge.net/projects/pydev/files/pydev/
注意: pydev 3.9必须使用java 7及以上版本,否则Eclipse无反应

2. 下载 pymssql-2.0.1.win32-py2.7.exe
https://pypi.python.org/pypi/pymssql/2.1.0
执行exe文件,python 安装目录下会出现_mssql.pyd 和 pymssql.pyd
C:\Python27\Lib\site-packages

3.测试

from pymssql import connect  # @UnresolvedImport

conn = connect(host="127.0.0.1", user="DataClient", password="test", 
               database="TimeSeries")
cursor = conn.cursor()
cursor.execute("exec getResearchSalesAndRepurchaseByYear %s, %d", ('F000000I1F',2011))
rows = cursor.fetchall()

for row in rows:
    print row[0]
    print row[1]
    
# cursor.execute("""
# IF OBJECT_ID('persons', 'U') IS NOT NULL
#     DROP TABLE persons
# CREATE TABLE persons (
#     id INT NOT NULL,
#     name VARCHAR(100),
#     salesrep VARCHAR(100),
#     PRIMARY KEY(id)
# )
# """)

# cursor.executemany(
#     "INSERT INTO persons VALUES (%d, %s, %s)",
#     [(1, 'John Smith', 'John Doe'),
#      (2, 'Jane Doe', 'Joe Dog'),
#      (3, 'Mike T.', 'Sarah H.')])
# # you must call commit() to persist your data if you don't set autocommit to True
# conn.commit()

# cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
# row = cursor.fetchone()
# while row:
#     print("ID=%d, Name=%s" % (row[0], row[1]))
#     row = cursor.fetchone()

conn.close()


在"from pymssql import connect"会提示错误:“unresolved import connect”, 这是静态代码检查报错。 解决方法,将鼠标放在报错的行,按“ctrl+1” 选择“unresolvedimport”, 会自动加入注释“# @UnresolvedImport”。

更多例子:
http://pymssql.org/en/latest/pymssql_examples.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26182553

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值