时间:2021-07-01 10:21:17 帮助过:26人阅读
完成odbc驱动的安装后,依赖环境就已经完成了
2、安装pyodbc
这个只要pip安装下就好了。
pip install pyodbc
3、如何使用pyodbc进行t-sql操作
1)简单查询
import pyodbc # Some other example server values are # server = ‘localhost\sqlexpress‘ # 实列名称 # server = ‘myserver,port‘ # 如果实例是有特殊端口号的,默认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)执行操作
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)如何使用window验证
cnxn = pyodbc.connect(‘DRIVER={ODBC Driver 17 for SQL Server};SERVER=‘+server+‘;DATABASE=‘+database+‘;Trusted_Connection=yes)
python操作数据库-SQLSERVER-pyodbc
标签:into host remove cts echo 根据 https lan epo