Thursday, August 18, 2016

Python code to interact with SQL server database

import pypyodbc

connection = pypyodbc.connect(r'Driver={SQL Server};Server=<Server_name>;Database=<Database_name>;Trusted_Connection=yes;')

cursor = connection.cursor()
SQLCommand = ("SELECT  TABLE_SCHEMA        ,TABLE_NAME       FROM    INFORMATION_SCHEMA.TABLES")
cursor.execute(SQLCommand)
results = cursor.fetchone()


while results:
     print ("Table Schema  " +  str(results[0]) + " Table name is " + results[1])
     results = cursor.fetchone()
connection.close()

No comments:

Post a Comment