问题
I am new to python. I am using Pydev IDE with Eclipse for python programming in my Windows machine. I am using python 3.3 vern and want to connect with MS Sql Server 2008. Could someone suggest how should I connect with MS Sql Server 2008.
回答1:
pyodbc supports python3 and can connect to any databas for wich there's an odbc driver, including sql server.
There's also a pure python implementation pypyodbc which also should supoort python3.
adodbapi also claims to work with python3.
Here you can find a list with some more options.
回答2:
I will augment mata's answer with a pypyodbc example.
import pypyodbc
connection_string ='Driver={SQL Server Native Client 11.0};Server=<YOURSERVER>;Database=<YOURDATABASE>;Uid=<YOURUSER>;Pwd=<YOURPASSWORD>;'
connection = pypyodbc.connect(connection_string)
SQL = 'SELECT * FROM <YOURTABLE>'
cur = connection.cursor()
cur.execute(SQL)
cur.close()
connection.close()
来源:https://stackoverflow.com/questions/17411362/connecting-python-3-3-to-microsoft-sql-server-2008