Tags

, , ,

Preconditions:

  1. Python 2.7.3 (32bit) installed
  2. Pymssql win32 module 2.0.0b1 module installed
  3. Microsoft® SQL Server® 2012 Native Client installed(search for “sqlncli.msi”)

Assuming that you have a table named: “user”, and it’s contains two columns: “firstname”, “lastname”, we can use the following code to retrieve the data by the following example code:

import _mssql
conn = _mssql.connect(server='dbhostIP', user='dbusername', password='dbpassword', database='dbname')
conn.execute_scalar("SELECT [firstname],[lastname] FROM [user]")
for row in conn:
    print "FirstName: %s, LastName: %s" % (row['firstname'],row['lastname'])
conn.close()

Result should be something like:

FirstName: MATTHEW, LastName: Carter
FirstName: Mi, LastName: Bal
FirstName: Mia, LastName: Bal
FirstName: Mia, LastName: Edwards
Advertisements