python 2.7中的MSSQL [英] MSSQL in python 2.7

查看:73
本文介绍了python 2.7中的MSSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可以连接 MSSQL 和 python 2.7 的模块?

Is there a module available for connection of MSSQL and python 2.7?

我下载了 pymssql 但它适用于 python 2.6.python 2.7 有没有等效的模块?

I downloaded pymssql but it is for python 2.6. Is there any equivalent module for python 2.7?

我不知道是否有人可以提供链接.

I am not aware of it if anyone can provide links.

重要提示:同时有一个 pymssql 模块可用.不要错过阅读本页末尾的答案:https://stackoverflow.com/a/25749269/362951

Important note: in the meantime there is a pymssql module available. Don't miss to read the answer at the end of this page: https://stackoverflow.com/a/25749269/362951

推荐答案

您也可以使用 pyodbc 从 Python 连接到 MSSQL.

You can also use pyodbc to connect to MSSQL from Python.

文档中的示例:

import pyodbc
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass')
cursor = cnxn.cursor()
cursor.execute("select user_id, user_name from users")
rows = cursor.fetchall()
for row in rows:
    print row.user_id, row.user_name

SQLAlchemy 库(在另一个答案中提到),使用 pyodbc 连接到 MSSQL 数据库(它尝试了各种库,但 pyodbc 是首选).使用 sqlalchemy 的示例代码:

The SQLAlchemy library (mentioned in another answer), uses pyodbc to connect to MSSQL databases (it tries various libraries, but pyodbc is the preferred one). Example code using sqlalchemy:

from sqlalchemy import create_engine
engine = create_engine("mssql://me:pass@localhost/testdb")
for row in engine.execute("select user_id, user_name from users"):
    print row.user_id, row.user_name

这篇关于python 2.7中的MSSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆