gpt4 book ai didi

python 连接到 Azure 数据库

转载 作者:行者123 更新时间:2023-12-01 06:23:52 25 4
gpt4 key购买 nike

我正在从 python 连接 Azure 数据库。这样做时我必须传递以下参数。我必须在“驱动程序”中指定为字符串。

enter code here

server = '<server>.database.windows.net'
database = '<database>'
username = '<username>'
password = '<password>'
driver= '{ODBC Driver 17 for SQL Server}'
cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].
[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid")
row = cursor.fetchone()
while row:
print (str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()

最佳答案

如果您的环境中安装了 Microsoft ODBC Driver 17。那么 driver 的值应为 driver = '{ODBC Driver 17 for SQL Server}'。您可以下载不同的ODBC驱动程序here .

工作 python 示例:

    import pyodbc
server = 'test.database.windows.net'
database = ''
username = ''
password = ''
driver = '{ODBC Driver 17 for SQL Server}'
cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server +
';PORT=1433;DATABASE='+database+';Uid=testsql;Pwd={your_password_here};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30')
cursor = cnxn.cursor()
cursor.execute(
"select * from Persons")
row = cursor.fetchone()
while row:
print(str(row[0]) + " " + str(row[1]))
row = cursor.fetchone()

关于python 连接到 Azure 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60259423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com