Python利用pymssql模块链接SQLServer数据库问题

1.本地数据库端口问题

本次讲述的是python利用 pymssql库链接sqlserver问题。

本小白初入Pyhon数据库坑,难免会遇到各种问题。比如:

pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (127.0.0.1:1433)\nNet-Lib error during Unknown error (10060)\n')
头一次创建好的数据库,创建好的表,却死活链接不上。

代码如下:

import pymssql
conn = pymssql.connect(host="127.0.0.1", 
                       user="sa",
                       password="123456",
                       database="Aircft_Wars",
                       charset="utf8")
# 查看连接是否成功
cursor = conn.cursor()
sql = "select * from 'User'"
sql= sql.replace("'","''")
cursor.execute(sql)
# 用一个rs变量获取数据
rs = cursor.fetchall()
print(type(rs),rs)

 如果出现该问题,通过网上查询,更多给出的是这种的解决方案:

但是到这一步就结束了,启用TCP/IP协议,就没有下文了。通过博主观察协议已经启用了。但是却没有什么用处。

其实应该点进去,操作如下:

 

 还有观察到有个端口问题,根据刚才的报错:does not exist (127.0.0.1:1433)

极有可能是端口问题,于是在原先python代码上加上如下改动:

import pymssql
conn = pymssql.connect(host="127.0.0.1:2301",
                       user="sa",
                       password="123456",
                       database="Aircft_Wars",
                       charset="utf8")
# 查看连接是否成功
cursor = conn.cursor()
sql = 'select * from "User"'
sql= sql.replace("'","''")
cursor.execute(sql)
# 用一个rs变量获取数据
rs = cursor.fetchall()
print(type(rs),rs)

 加上了端口号程序运行如下:

代码运行成功,问题解决。


 

发布了9 篇原创文章 · 获赞 12 · 访问量 3969
展开阅读全文

求救 pymssql怎么都连不上 服务中sql server代理也启动不了

01-14

``` Traceback (most recent call last): File "pymssql.pyx", line 630, in pymssql.connect (pymssql.c:10097) File "_mssql.pyx", line 1887, in _mssql.connect (_mssql.c:20477) File "_mssql.pyx", line 631, in _mssql.MSSQLConnection.__init__ (_mssql.c:6156) File "_mssql.pyx", line 1612, in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:16426) _mssql.MSSQLDatabaseException: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist\nNet-Lib error during Unknown error (10035)\n') During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Python33/sqltest.py", line 3, in <module> conn = pymssql.connect(host = r"localhost", port = r'1433', user = r'sa', password = r'412765442', database = r'test') File "pymssql.pyx", line 636, in pymssql.connect (pymssql.c:10178) pymssql.OperationalError: (20009, b'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist\nNet-Lib error during Unknown error (10035)\n') ``` 一晚上都弄不好啊! 真烦!错误提示在上面 ``` import pymssql conn = pymssql.connect(host = r"localhost", port = r'1433', user = r'sa', password = r'412765442', database = r'test') cur = conn.cursor() if not cur: raise(NameError,"连接数据库失败") print("====================================") reslist = cur.execute("select * from dbo.person_table") for (id, name) in reslist: print(str(id), name) ``` 以上是代码 求救啊! sql server代理在启动时也会失败!![图片说明](https://img-ask.csdn.net/upload/201501/14/1421250090_312788.jpg)[图片说明](https://img-ask.csdn.net/upload/201501/14/1421250116_182658.jpg) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览