pymssql 安装与SQL Server连接问题处理

pymssql 安装与 SQL Server连接

一、下载pymssql-2.1.0-py2.6-win32.egg (md5)     ,放到python的script目录下,运行easy_install pymssql-2.1.0-py2.6-win32.egg (md5)     安装

二、http://pymssql.org/en/latest/pymssql_examples.html这里有连接的例子,一开始的时候照着也是连接不上,要打开SQL Server的网络连接。打开SQL Server的配置管理器,把网络配置里的TCP/IP启用。

三、中文的处理。要用encode('utf8'),如:commnad = commnad.encode('utf8')

官方例子:

from os importgetenv
import pymssql

server = getenv("PYMSSQL_TEST_SERVER")
user = getenv("PYMSSQL_TEST_USERNAME")
password = getenv("PYMSSQL_TEST_PASSWORD")

conn = pymssql.connect(server,user, password,"tempdb")
cursor = conn.cursor()
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()








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值