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

Article with comments

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× | Add comment

Comments (0)

No comments for this article yet.

Write a comment

Your name  
E-mail   (only visible for blog owner)
Homepage
Text:

[b] [i] [u] [tt] [center] [code] [quote] [url] [url=] [img] [@] [@@] [@:]
detailed help about markup
You must answer the following to be able to submit.
How much is four times seven?  
[Captcha Image] Type the letters you see in the image.
(Unreadable? Click on it for another one)

Process times: page=0.006 request=0.011 cpu=0.009