简体   繁体   中英

Connecting python 3.3 to microsoft sql server 2008

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.

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()

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.

import pyodbc
server = 'SERVIDORNOMEOUIP'
database = 'MEUBANCO'
username = 'USERSQL'
password = 'SENHASQL'

#for SQL Server 2008
driver='{SQL Server Native Client 10.0}'

cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password + ';')

cursor = cnxn.cursor()

cursor.execute("SELECT nome,senha FROM [tabusuariosenha]")

row = cursor.fetchone()
print ("CAMPO1  |  CAMPO2 " )
while row:
    print (str(row[0]) + " " + str(row[1]))
    row = cursor.fetchone()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM