在MacOS上使用python安装pymssql库
# 在MacOS上使用python安装pymssql库
本实例介绍如何在MacOS系统中,使用python上连接SqlServer数据库。
pymssql是一个python操作SqlServer的库
# 安装pymssql
安装pymssql的顺序:unixodbc-->freetds--->pymsssql
# 关闭brew的自动更新
关闭brew的自动更新,否则brew install的时候卡死在Updating Homebrew...界面上
在命令行中输入如下命令
export HOMEBREW_NO_AUTO_UPDATE=true
1
# 安装unixodbc
brew install unixodbc
1
# 安装freetds
brew install freetds --with-unixodbc
1
注意:这里一定要使用--with-unixodbc选项
# 安装pymssql库
安装pymssql库的时候请按如下步骤安装
brew uninstall --force freetds
brew install freetds@0.91
brew link --force freetds@0.91
pip install pymssql
1
2
3
4
2
3
4
注意:
其中link --force freetds@0.91这步骤如果出错,则把相应已经存在的文件删除(移动)即可
例如:如下例子中,提示/usr/local/etc/freetds.conf文件已存在,则把该文件从该目录删除记录。
这里我为了安全,直接mv移动到别的目录了。
fangzheng@localhost $ brew link --force freetds@0.91
Linking /usr/local/Cellar/freetds@0.91/0.91.112...
Error: Could not symlink etc/freetds.conf
Target /usr/local/etc/freetds.conf
already exists. You may want to remove it:
rm '/usr/local/etc/freetds.conf'
To force the link and overwrite all conflicting files:
brew link --overwrite freetds@0.91
To list all files that would be deleted:
brew link --overwrite --dry-run freetds@0.91
fangzheng@localhost $ mv /usr/local/etc/freetds.conf ~/
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# pymssql简单使用
具体使用方法可参考官网 (opens new window)
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
@version : v1.0
@author : fangzheng
@contact : 13070108748@163.com
@software : PyCharm
@filename : inc_data.py
@create time: 2018/7/26 下午2:15
@describe : 使用python+datax增量抽取SQL Server数据到MySQL
-------------------------------------------------
"""
import pymssql
# jdbc:sqlserver://10.15.1.11:2121;databaseName=pms
try:
conn = pymssql.connect(host="192.168.1.1", user="username", password="pwd", database="db", port=2121, charset="utf8")
cursor = conn.cursor()
sql = "SELECT updatestamp,cast(updatestamp as bigint) FROM tablename;"
cursor.execute(sql)
rows = cursor.fetchone()
# rows = cursor.fetchall()
# for row in rows:
# print row[0]
print rows
# except Exception as e:
# print e
finally:
if cursor:
cursor.close()
if conn:
conn.close()
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
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
最后更新时间: 2022/7/23 10:17:11