Spinsels op het web
actions » SearchLogin 310 articles • 07 Feb 2012

Articles written on 05 Apr 2010

Monday, 05 Apr 2010

permalink Python en MS SQLServer

Omdat ik er altijd mee loop te klooien, hier even een notitie hoe je vanuit Python naar Microsoft SQLServer kunt connecten. Ik heb het getest met SQLServer 2005 express edition.

Met adodbapi, onderdeel van Pywin32 extensions van Mark Hammond:

import adodbapi
conn=adodbapi.connect("Provider=sqloledb;Data Source=(local)\\SQLEXPRESS; Initial Catalog=Test;Integrated Security=SSPI")
cursor=conn.cursor()
cursor.execute("select * from Person")
print "result: %d rows (-1=unknown)" % cursor.rowcount
results=cursor.fetchmany(10)
print "first 10 results:"
for record in results:
    print record
cursor.execute("insert into Person(voornaam,achternaam) VALUES (?,?)", ("test","another person"))
conn.commit()
cursor.close()
conn.close()

Of via een kleinere package, Pymssql:

import pymssql
conn=pymssql.connect(host="(local)\\SQLEXPRESS", database="Test", trusted=True)
cursor=conn.cursor()
cursor.execute("select * from Person")
print "result: %d rows (-1=unknown)" % cursor.rowcount
results=cursor.fetchmany(10)
print "first 10 results:"
for record in results:
	  print record
cursor.execute("insert into Person(voornaam,achternaam) VALUES (%s,%s)", ("test","another person"))
conn.commit()
cursor.close()
conn.close()
• Wrote irmen at 02:27 | read 19× | 0 Comments

1 shown; more articles may be found in the archives. The permalink icon is the article's permalink.
Process times: page=0.003 request=0.007 cpu=0.006