Python operation database-SQLSERVER-pyodbc

When we first started learning python, everyone used to use pymssql to read and write SQLSERVER. But in the actual use process, pymssql's reading and writing performance and reliability are indeed not as good as pyodbc.

As Microsoft officially recommends using the pyodbc library, it is used as a learning and production environment. Reference link  https://docs.microsoft.com/en-us/sql/connect/python/python-driver-for-sql-server?view=sql-server-2017  

 

The first step is to configure the development environment for pyodbc Python development.

1. Install the Microsoft ODBC SQL Server driver on Windows

window install as needed

Download Download Microsoft ODBC Driver 17 for SQL Server (x64)
Download Download Microsoft ODBC Driver 17 for SQL Server (x86)

Linux takes centos as an example

Reference link  https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-2017# redhat17

sudo  su

#Download appropriate package for the OS version
#Choose only ONE of the following, corresponding to your OS version

#RedHat Enterprise Server 6
curl https://packages.microsoft.com/config/rhel/6/prod.repo > /etc/yum.repos.d/mssql-release.repo

#RedHat Enterprise Server 7
curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo

#RedHat Enterprise Server 8 and Oracle Linux 8
curl https://packages.microsoft.com/config/rhel/8/prod.repo > /etc/yum.repos.d/mssql-release.repo

exit
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel #to avoid conflicts
sudo ACCEPT_EULA=Y yum install msodbcsql17
# optional: for bcp and sqlcmd
sudo ACCEPT_EULA=Y yum install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
# optional: for unixODBC development headers
sudo yum install unixODBC-devel

After completing the installation of the odbc driver, the dependent environment has been completed

2. Install pyodbc

This is as long as pip is installed.

pip install pyodbc

3. How to use pyodbc for t-sql operation

1) Simple query

import pyodbc 
# Some other example server values are
# server = ' localhost \ sqlexpress ' # Real column name
# server = ' myserver, port ' # If the instance has a special port number, the default is 1433
server = 'tcp:myserver.database.windows.net' 
database = 'mydb' 
username = 'myusername' 
password = 'mypassword' 
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor ()

cursor.execute("SELECT @@version;") 
row = cursor.fetchone() 
while row: 
    print(row[0])
    row = cursor.fetchone()

2) Perform the operation

cursor.execute("""
INSERT INTO SalesLT.Product (Name, ProductNumber, StandardCost, ListPrice, SellStartDate) 
VALUES (?,?,?,?,?)""",
'SQL Server Express New 20', 'SQLEXPRESS New 20', 0, 0, CURRENT_TIMESTAMP) 
cnxn.commit()
row = cursor.fetchone()

while row: 
    print('Inserted Product key is ' + str(row[0]))
    row = cursor.fetchone()

3) How to use window authentication

cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';Trusted_Connection=yes)

 

Guess you like

Origin www.cnblogs.com/Evan-fanfan/p/12694287.html