SQL Server 2022 : Use with Python2023/12/15 |
This is an example to use SQL Server with Python.
|
|
[1] | This is based on the environment Python 3 has been installed. Additionally, Install Python DB API for ODBC. |
root@dlp:~# apt -y install python3-pyodbc
|
[2] | Create a sample User and Database for Test. |
root@dlp:~# sqlcmd -S localhost -U SA Password: # create login user 1> create login debian with PASSWORD= N'P@ssw0rd01'; 2> go # create [SampleDB3] 1> create database SampleDB3; 2> go 1> use SampleDB3; 2> go Changed database context to 'SampleDB'. # create DB user 1> create user debian for login debian; 2> go # assign DB owner role to [debian] 1> exec sp_addrolemember 'db_owner', 'debian'; 2> go # create [SampleTable] 1> create table SampleTable ( 2> ID int identity(1,1) not null primary key, First_Name NVARCHAR(50), Last_Name NVARCHAR(50) ); 3> insert into SampleTable ( 4> First_Name, Last_Name) values (N'Ubuntu', N'Linux'), (N'Debian', N'Linux'), (N'RedHat', N'Linux' ); 5> go |
[3] | This is a basic usage example of SQL Server on Python. It uses database and connection user that were created above. |
# confirm ODBC Driver version debian@dlp:~$ odbcinst -j unixODBC 2.3.11 DRIVERS............: /etc/odbcinst.ini SYSTEM DATA SOURCES: /etc/odbc.ini FILE DATA SOURCES..: /etc/ODBCDataSources USER DATA SOURCES..: /home/debian/.odbc.ini SQLULEN Size.......: 8 SQLLEN Size........: 8 SQLSETPOSIROW Size.: 8debian@dlp:~$ cat /etc/odbcinst.ini [ODBC Driver 17 for SQL Server] Description=Microsoft ODBC Driver 17 for SQL Server Driver=/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.5.1 UsageCount=1
debian@dlp:~$
vi use_mssql.py import pyodbc server = '127.0.0.1' database = 'SampleDB3' username = 'debian' password = 'P@ssw0rd01' cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server}; \ SERVER='+server+'; PORT=1443; DATABASE='+database+'; UID='+username+'; PWD='+ password) cursor = cnxn.cursor() # Select data from SampleTable print ('\nReading data from SampleTable') tsql = "select * from SampleTable;" with cursor.execute(tsql): row = cursor.fetchone() while row: print (str(row[0]) + " " + str(row[1]) + " " + str(row[2])) row = cursor.fetchone() # Insert data into SampleTable print ('\nInserting a new row into SampleTable') tsql = "insert into SampleTable (First_Name, Last_Name) values (?,?);" with cursor.execute(tsql,'Windows','Microsoft'): print ('- Successfuly Inserted!') # Update data in SampleTable print ('\nUpdating Last_Name for Redhat') tsql = "update SampleTable set Last_Name = ? where First_Name = ?" with cursor.execute(tsql,'Plow','Redhat'): print ('- Successfuly Updated!') tsql = "select * from SampleTable;" with cursor.execute(tsql): row = cursor.fetchone() while row: print (str(row[0]) + " " + str(row[1]) + " " + str(row[2])) row = cursor.fetchone() # Delete data in SampleTable print ('\nDeleting user Windows') tsql = "delete from SampleTable where First_Name = ?" with cursor.execute(tsql,'Windows'): print ('- Successfuly Deleted!') python3 use_mssql.py Reading data from SampleTable 1 Ubuntu Linux 2 Debian Linux 3 RedHat Linux Inserting a new row into SampleTable - Successfuly Inserted! Updating Last_Name for Redhat - Successfuly Updated! 1 Ubuntu Linux 2 Debian Linux 3 RedHat Plow 4 Windows Microsoft Deleting user Windows - Successfuly Deleted! |
Sponsored Link |