python 连接到 Azure 数据库

标签 python azure azure-sql-database azure-functions

我正在从 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/

相关文章:

OMS 门户中的 Azure SQL Analytics 磁贴未立即显示最新数据库?

c# - SQL 数值列禁止间隙

Python 解析 JSON 数组

python - 使用正则表达式替换特定匹配项

python - 如何使用 shell 命令将目录作为参数传递给 python 脚本?

azure - 使用 Next-Auth 身份验证从 Azure Active Directory 获取配置文件图像

Azure Sql 数据仓库 API

python - 在Python中,单 channel 图像的形状是一个问题

sql-server - Azure 恢复服务和 SQL 2014 托管备份不能很好地协同工作

razor - 为什么我的 servicestack 站点的 Razor View 仅在 Azure 上显示元数据页面?