Domanda

C'è un modulo per il collegamento di MSSQL e pitone 2.7?

Ho scaricato pymssql ma è per Python 2.6. Esiste un modulo equivalente per Python 2.7?

Non sono a conoscenza di esso se qualcuno può fornire link.


Nota importante: nel frattempo c'è un modulo pymssql disponibili. Non mancate di leggere la risposta alla fine di questa pagina: https://stackoverflow.com/a/25749269/362951

È stato utile?

Soluzione

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

An example from the documentation:

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

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

Altri suggerimenti

If you're coming across this question through a web search, note that pymssql nowadays does support Python 2.7 (and 3.3) or newer. No need to use ODBC.

From the pymssql requirements:

Python 2.x: 2.6 or newer. Python 3.x: 3.3 or newer.

See http://pymssql.org/.

Install pyodbc using pip as follows: pip install pyodbc

import pyodbc
cnxn = pyodbc.connect("DRIVER={SQL Server};SERVER=SOME-PC;DATABASE=my_db")
cursor = cnxn.cursor()


cursor.execute("insert into test_tb values(6, 'name')")

cursor.execute("select id, name from my_tb")
rows = cursor.fetchall()
for row in rows:
    print row.id, row.name

For details, see

https://github.com/mkleehammer/pyodbc/wiki

You can try out SQLAlchemy: The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes (objects) with rows in their corresponding tables.

You can refer following links: 1> http://www.sqlalchemy.org/docs/ 2> http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top