Python DB connection MSSQL

 WE can by using any  one of the following drivers, 

Pip instal pymssql


The pymssql.connect function is used to connect to SQL Database.

Sql results will be displayed in tuples

cursor.execute function

Cursor objects are used to streamline the connection between the puthon and sql server,

execute SQL statements

This function accepts a query and returns a result set

can be iterated over with the use of cursor.fetchone()

pip install pyodbcimport pyodbc

# Some other example server values are
# server = 'localhost\sqlexpress' # for a named instance
# server = 'myserver,port' # to specify an alternate port
server = '**********.database.windows.net'
database = '********'
username = '*******'
password = '******@easapi$'
cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("SELECT * FROM [dbo].[User] where LastName='Reddy'")
row = cursor.fetchone()# it will retrive list[0]
print(row[4])# it will retrieve 4th idex
print(type(row))
#print(row)
#print(row[5])
while row:
# print(row)
#print(type(row))
row = cursor.fetchone()


 import pymssql

Pip instal pymssql



conn = pymssql.connect(server='*********.database.windows.net',
user='******',
password='*******@easapi$', database='*******')

cursor = conn.cursor()
cursor.execute('select * from users')
row = cursor.fetchone()
while row:
print(str(row[0]) + " " + str(row[1]) + " " + str(row[2]))
row = cursor.fetchone()

Comments

Popular posts from this blog

Implicit and Explicit Waits,FluentWait,PageLoadTimeOut

A Interview Questions- selenium