python如何连接sql server数据库_Python连接SQLServer数据库

1.安装pymssql

(1) 使用pip3 install pymsslq出现下面这个问题:

ERROR: Could not build wheels for pymssql which use PEP 517 and cannot be installed directly

使用命令:

(2) pip3 install “pymssql<3.0” 也是不行的

(3) 最后只能在python2下使用pip install “pymssql<3.0”(我安装了python2和3共存)

2.数据库中文名的问题

因为数据名称中包含中文,所以在连接时,总是报错:UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 5-8: ordinal not in range(128)。

我尝试了在:python的Lib\site-packages文件夹下新建一个sitecustomize.py

1

2

3

4

5

6#!/usr/bin/python

# -*- coding: UTF-8 -*-

import sys

reload(sys)

sys.setdefaultencoding('utf8')

虽然系统最后的默认编码变成了utf8,但是上面的错误还是没有消除。

即便我将中文写成utf-8字符串,还是不行。

1

2

3

4

5database=u'\u534E\u7535\u4E0B\u6C99'

print(database)

#数据库远程连接

## conn = pymssql.connect(host="127.0.0.1",user="admin",password="1q2w3e4r.",database="test",charset="utf8")

conn = pymssql.connect(host="****",user="****",password="****",database=database,charset="utf8")

unicode字符可以在网站中进行转换。将之后,分号之前的字符作为unicode字符。

最后只能改数据库名好了啊

2020年08月21日更新

最近看了一篇文章,就是使用了另一种方式,使用了先连接master,然后再使用use的sql语句进行查询的方法。借鉴参考文章中的方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19import pymssql

import pprint

server = "127.0.0.1\\sql2000"

port = '1434'

user = "sa"

password = "123456"

database = 'master'

conn = pymssql.connect(server=server, user=user, password=password, database=database, port=port, charset="utf8")

cursor = conn.cursor()

sql = "use %s " % 'S3中文名' #注意%s后面有一个空格

sql += "select top 5 * from d_goods "

cursor.execute(sql)

rows = cursor.fetchall()

print(rows)

conn.close()

3.DB-Lib error message20009

pymssql.OperationalError: (20009, ‘DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (120.199.181.229:5433)\nNet-Lib error during Unknown error (10060)\n’)

需要把SQL Server的TCP/IP访问打开

4.连接代码1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41#!/usr/bin/python

# -*- coding: utf-8 -*-

"""

Created on Mon Dec 2 15:11:35 2019

@author: ph

"""

import pymssql

import sys

import io

sys.setdefaultencoding('utf8')

# setup_io()

class LinkDB():

def linkdb(self):

#数据库远程连接

conn = pymssql.connect(host="****(无端口)",user="****",password="****",database="****",charset="utf8")

# 使用cursor()方法获取操作游标

cursor = conn.cursor()

#查询语句

# sql = "SELECT * FROM [dbo].[test]"

# sql="SELECT * FROM [dbo].[V_client]"

try:

cursor.execute(sql) #游标

result = cursor.fetchone() #查询

print(result)

except:

print("error")

#cursor.close()

#关闭数据库连接

conn.close()

if __name__ == '__main__':

link=LinkDB()

link.linkdb()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值