import pymssql
conn=pymssql.connect(host='127.0.0.1:1433',user='sa',password='jxn',database='scott',charset="UTF-8")
'''
如果和本机数据库交互,只需修改链接字符串
conn=pymssql.connect(host='.',database='Michael')
'''
cur=conn.cursor()
cur.execute('select * from emp')
#如果update/delete/insert记得要conn.commit()
#否则数据库事务无法提交
print(cur.fetchall())
cur.close()
conn.close()
一开始运行上面代码老是出现以下错误:
Traceback (most recent call last):
File "src\pymssql.pyx", line 636, in pymssql.connect
File "src\_mssql.pyx", line 1952, in _mssql.connect
File "src\_mssql.pyx", line 673, in _mssql.MSSQLConnection.__init__
File "src\_mssql.pyx", line 1680, in _mssql.maybe_raise_MSSQLDatabaseException
_mssql.MSSQLDatabaseException: (20009, b'Unknown error')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "
conn=pymssql.connect(host='127.0.0.1',user='sa',password='jxn',database='scott',charset='utf-8')
File "src\pymssql.pyx", line 642, in pymssql.connect
pymssql.OperationalError: (20009, b'Unknown error')
查看了网上很多的资料,都找不到答案,添加了端口号:没用,添加了charset:没用,TCP/IP开启:没用,
有的网友说要不试一下用pyodbc进行数据库连接?好吧,我不想放弃,就又去安装pyodbc,并找了段测试代码:
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Native Client};SERVER=127.0.0.1,1433;DATABASE=scott;UID=sa;PWD=jxn')
cursor = cnxn.cursor()
cursor.execute("select * from emp")
rows = cursor.fetchall()
for row in rows:
print(row)
运行结果是如下错误:
Traceback (most recent call last):
File "D:/Python362/a_数据库/2.py", line 2, in
cnxn = pyodbc.connect('DRIVER={SQL Native Client};SERVER=127.0.0.1,1433;DATABASE=scott;UID=sa;PWD=jxn')
pyodbc.OperationalError: ('08001', '[08001] [Microsoft][SQL Native Client]TCP 提供程序: 由于目标计算机积极拒绝,无法连接。\r\n (10061) (SQLDriverConnect); [08001] [Microsoft][SQL Native Client]登录超时已过期 (0); [08001] [Microsoft][SQL Native Client]建立到服务器的连接时发生错误。连接到 SQL Server 2005 时,默认设置 SQL Server 不允许远程连接这个事实可能会导致失败。 (10061)')
不管什么办法就是调不通,万般无奈之下,看了有的博客说将防火墙检查一下,就是以下这篇博客:点击打开链接
我抱着试试看的心里,结果还真成功了!!!
我的详细设置过程是:
第一步:在开始菜单中输入:Firewall.cpl,然后点击该程序
点击允许程序或功能通过Windows防火墙,你会发现里面没有SQL Server,那就需要我们手动添加,将添加路径指定到你安装数据库的文件夹里,并在文件夹里找到sqlserver.exe
添加完了之后你就会发现SQL Server也在里面了
然后点击确定,防火墙这一块就算结束了。
下面是验证代码:
import pymssql
conn=pymssql.connect(host='127.0.0.1:1433',user='sa',password='jxn',database='scott',charset="UTF-8")
'''
如果和本机数据库交互,只需修改链接字符串
conn=pymssql.connect(host='.',database='Michael')
'''
cur=conn.cursor()
cur.execute('select * from emp')
#如果update/delete/insert记得要conn.commit()
#否则数据库事务无法提交
print(cur.fetchall())
cur.close()
conn.close()
输出结果为:
====================== RESTART: D:/Python362/a_数据库/1.py ======================
[(7369, 'SMITH', 'CLERK', 7902, '1981-12-17', 800, None, 20), (7499, 'ALLEN', 'SALESMAN', 7698, '1981-2-20', 1600, 300, 30), (7521, 'WARD', 'SALESMAN', 7698, '1981-2-22', 1250, 500, 30), (7566, 'JONES', 'MANAGER', 7839, '1981-4-2', 2975, None, 20), (7654, 'MARTIN', 'SALESMAN', 7698, '1981-9-28', 1250, 1400, 30), (7698, 'BLAKE', 'MANAGER', 7839, '1981-5-1', 2850, None, 30), (7782, 'CLARK', 'MANAGER', 7839, '1981-6-9', 2450, None, 10), (7788, 'SCOTT', 'ANALYST', 7566, '1981-4-19', 3000, None, 20), (7839, 'KING', 'PRESIDENT', None, '1981-11-17', 5000, None, 10), (7844, 'TURNER', 'SALESMAN', 7698, '1981-9-8', 1500, 0, 30), (7876, 'ADAMS', 'CLERK', 7788, '1987-5-23', 1100, None, 20), (7900, 'JAMES', 'CLERK', 7698, '1981-12-3', 950, None, 30), (7902, 'FORD', 'ANALYST', 7566, '1982-12-3', 3000, None, 20), (7934, 'MILLER', 'CLERK', 7782, '1982-1-23', 1300, None, 10)]
另一种数据库连接方法:
import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Native Client};SERVER=127.0.0.1,1433;DATABASE=scott;UID=sa;PWD=jxn')
cursor = cnxn.cursor()
cursor.execute("select * from emp")
rows = cursor.fetchall()
for row in rows:
print(row)
输出结果为:
====================== RESTART: D:/Python362/a_数据库/2.py ======================
(7369, 'SMITH', 'CLERK', 7902, '1981-12-17', 800, None, 20)
(7499, 'ALLEN', 'SALESMAN', 7698, '1981-2-20', 1600, 300, 30)
(7521, 'WARD', 'SALESMAN', 7698, '1981-2-22', 1250, 500, 30)
(7566, 'JONES', 'MANAGER', 7839, '1981-4-2', 2975, None, 20)
(7654, 'MARTIN', 'SALESMAN', 7698, '1981-9-28', 1250, 1400, 30)
(7698, 'BLAKE', 'MANAGER', 7839, '1981-5-1', 2850, None, 30)
(7782, 'CLARK', 'MANAGER', 7839, '1981-6-9', 2450, None, 10)
(7788, 'SCOTT', 'ANALYST', 7566, '1981-4-19', 3000, None, 20)
(7839, 'KING', 'PRESIDENT', None, '1981-11-17', 5000, None, 10)
(7844, 'TURNER', 'SALESMAN', 7698, '1981-9-8', 1500, 0, 30)
(7876, 'ADAMS', 'CLERK', 7788, '1987-5-23', 1100, None, 20)
(7900, 'JAMES', 'CLERK', 7698, '1981-12-3', 950, None, 30)
(7902, 'FORD', 'ANALYST', 7566, '1982-12-3', 3000, None, 20)
(7934, 'MILLER', 'CLERK', 7782, '1982-1-23', 1300, None, 10)
有什么疑问可以加我QQ讨论一下:1148540694
以下是开启TCP/IP协议以及查看默认端口的过程!!!
要想运行成功,前提条件还需要将TCP/IP协议开启,下面附上开启过程以及查看端口的过程
右击TCP/IP选择属性
TCP动态端口我的一开始是0,TCP端口是空,看了网上教程说需要修改,然后我将TCP动态端口改为空,TCP端口改为默认端口1433,默认端口可以通过以下步骤查看,待会儿讲解,最后按确定。
以下步骤是查看TCP默认端口:SQL Native Client配置->客户端协议->TCP/IP->属性