浅墨散人 浅墨散人
  • 基础
  • 设计模式
  • JVM
  • Maven
  • SpringBoot
  • 基础
  • Flask
  • Diango
  • Pandas
  • SqlAlchemy
  • Sqoop
  • Flume
  • Flink
  • Hadoop
  • Hbase
  • Hive
  • Kafka
  • Kylin
  • Zookeeper
  • Tez
  • MySQL
  • Doris
  • Chrome
  • Eclipse
  • IDEA
  • iTerm2
  • Markdown
  • SublimeText
  • VirtualBox
  • WebStrom
  • Linux
  • Mac
  • Hexo
  • Git
  • Vue
  • VuePress
  • 区块链
  • 金融
数据仓库
数据治理
读书笔记
关于我
GitHub (opens new window)
  • 基础
  • 设计模式
  • JVM
  • Maven
  • SpringBoot
  • 基础
  • Flask
  • Diango
  • Pandas
  • SqlAlchemy
  • Sqoop
  • Flume
  • Flink
  • Hadoop
  • Hbase
  • Hive
  • Kafka
  • Kylin
  • Zookeeper
  • Tez
  • MySQL
  • Doris
  • Chrome
  • Eclipse
  • IDEA
  • iTerm2
  • Markdown
  • SublimeText
  • VirtualBox
  • WebStrom
  • Linux
  • Mac
  • Hexo
  • Git
  • Vue
  • VuePress
  • 区块链
  • 金融
数据仓库
数据治理
读书笔记
关于我
GitHub (opens new window)
  • Python基础

    • Python基础
    • Python基础
    • 在MacOS上安装Python2和Python3
    • 在MacOS上使用python安装pymssql库
      • 在MacOS上使用python安装pymssql库
      • 安装pymssql
        • 关闭brew的自动更新
        • 安装unixodbc
        • 安装freetds
        • 安装pymssql库
      • pymssql简单使用
    • 使用Python构建交互式的Dashboard
    • Python一行代码实现文件共享服务器
    • Python浏览器自动化工具splinter
    • Python将txt转为xlsx文件
    • Python的virtualenv虚拟环境
  • Python
  • Basic
2018-07-26
目录

在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

注意:

其中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

# 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
#Python
最后更新时间: 2022/7/23 10:17:11
在MacOS上安装Python2和Python3
使用Python构建交互式的Dashboard

← 在MacOS上安装Python2和Python3 使用Python构建交互式的Dashboard→

最近更新
01
分区分桶
08-21
02
数据模型(重要)
08-21
03
安装和编译
08-21
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式