前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >在Mac/Linux系统下安装pymssql模块

在Mac/Linux系统下安装pymssql模块

作者头像
凡梦星尘
发布2024-11-20 17:25:05
发布2024-11-20 17:25:05
1120
举报
文章被收录于专栏:Hugo博客Hugo博客

在非Windows环境下去访问,连接 MSSQL 数据,本身就是件苦差事来的。自写Python程序以来在ORM方面都是使用pyxxx的模块,果不其然连接 MSSQL 也有个模块叫pymssql,只是实际使用中并不是特别的顺利。如笔者所处的环境就是如此,开发环境为OSX 10.11,发布环境为CentOS 6.4,按官方的安装步骤实行下来,Linux 环境是OK的,只是 Mac 环境下安装失败,错误的堆栈信息如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Running setup.py install for pymssql ... error Complete output from command /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-KA5ksi/pymssql/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-A3wRBy-record/install-record.txt --single-version-externally-managed --compile: setup.py: platform.system() => 'Darwin' setup.py: platform.architecture() => ('64bit', '') setup.py: platform.libc_ver() => ('', '') setup.py: Detected Darwin/Mac OS X. You can install FreeTDS with Homebrew or MacPorts, or by downloading and compiling it yourself. Homebrew (http://brew.sh/) -------------------------- brew install freetds MacPorts (http://www.macports.org/) ----------------------------------- sudo port install freetds ...... /usr/bin/clang -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -g -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/opt/local/include -I/opt/local/include/freetds -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mssql.c -o build/temp.macosx-10.6-intel-2.7/_mssql.o -DMSDBLIB _mssql.c:18924:15: error: use of undeclared identifier 'DBVERSION_80' __pyx_r = DBVERSION_80;

在安装pymssql之前有个关于的组件为FreeTDS,所遇到的问题也就是出现在此组件上面。 在LinuxOSX环境下的安装命令分别如下:

1 2 3 4 5

# Linux yum install freetds-devel.x86_64 # Mac brew install freetds

Mac环境中需要注意freetds的版本引起的问题,可以正常使用的版本为0.91,修正后的安装命令如下:

1 2 3

brew uninstall --force freetds brew install freetds@0.91 brew link --force freetds@0.91

另外还得需要安装一个Python模块,安装命令如下:

1

pip install cython

上述环境准备就绪后,便可以顺利的安装pymssql模块,执行如下安装命令:

1

pip install pymssql

写个简单的测试代码如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

#!/usr/bin/env python # -*- coding: utf_8 -*- # coding=utf8 import pymssql server = "192.168.1.2" user = "sa" password = "123456" conn = pymssql.connect(server, user, password, database="platform") cursor = conn.cursor() cursor.execute("SELECT * FROM Table") row = cursor.fetchone() while row: row = cursor.fetchone() print row conn.close()

OK,全部搞定,继续码代码去。

参考如下:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档