0

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.

Mainak
  • 311
  • 2
  • 4
  • 7

3 Answers3

10

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()
Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
joshpierro
  • 216
  • 3
  • 6
5

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.

mata
  • 67,110
  • 10
  • 163
  • 162
1
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()
GTIcontrol
  • 36
  • 2