Skip to main content

Azure ODBC Client

一个简单易用的Azure SQL Server ODBC客户端包,专为Python开发者设计。

功能特性

  • 🔗 简单连接: 轻松连接到Azure SQL Server数据库
  • 🔐 多种认证: 支持SQL Server认证和Azure Active Directory认证
  • 📊 数据查询: 执行SQL查询并获取结构化数据
  • 🛡️ 错误处理: 完善的异常处理和日志记录
  • 🔧 参数化查询: 支持安全的参数化查询
  • 📋 表信息: 获取数据库表结构和表列表
  • 🧪 连接测试: 内置连接测试功能

安装

pip install azure-odbc-client

快速开始

基本使用

from azure_odbc_client import AzureODBCClient

# 创建客户端实例
client = AzureODBCClient(
    server="yourserver.database.windows.net",
    database="your_database",
    username="your_username",
    password="your_password"
)

# 测试连接
if client.test_connection():
    print("连接成功!")

# 执行查询
results = client.execute_query("SELECT * FROM users WHERE age > ?", [18])
for row in results:
    print(f"用户: {row['name']}, 年龄: {row['age']}")

使用Azure Active Directory认证

from azure_odbc_client import AzureODBCClient

# 使用Azure AD认证
client = AzureODBCClient(
    server="yourserver.database.windows.net",
    database="your_database",
    use_azure_auth=True  # 启用Azure AD认证
)

# 执行查询
results = client.execute_query("SELECT COUNT(*) as total FROM products")
print(f"产品总数: {results[0]['total']}")

执行非查询操作

# 插入数据
affected_rows = client.execute_non_query(
    "INSERT INTO users (name, email, age) VALUES (?, ?, ?)",
    ["张三", "zhangsan@example.com", 25]
)
print(f"插入了 {affected_rows} 行数据")

# 更新数据
affected_rows = client.execute_non_query(
    "UPDATE users SET age = ? WHERE name = ?",
    [26, "张三"]
)
print(f"更新了 {affected_rows} 行数据")

获取数据库信息

# 获取所有表名
tables = client.get_database_tables()
print("数据库中的表:", tables)

# 获取表结构信息
table_info = client.get_table_info("users")
for column in table_info:
    print(f"列名: {column['COLUMN_NAME']}, 类型: {column['DATA_TYPE']}")

高级用法

使用连接上下文管理器

# 手动管理连接
with client.get_connection() as conn:
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users")
    results = cursor.fetchall()
    # 连接会自动关闭

配置日志

import logging

# 配置日志级别
logging.basicConfig(level=logging.INFO)
client = AzureODBCClient(
    server="yourserver.database.windows.net",
    database="your_database",
    username="your_username",
    password="your_password"
)

API 参考

AzureODBCClient

构造函数参数

  • server (str): Azure SQL Server地址
  • database (str): 数据库名称
  • username (str, 可选): 用户名(SQL认证时必需)
  • password (str, 可选): 密码(SQL认证时必需)
  • driver (str): ODBC驱动程序名称,默认为"ODBC Driver 17 for SQL Server"
  • timeout (int): 连接超时时间(秒),默认30秒
  • autocommit (bool): 是否自动提交事务,默认True
  • use_azure_auth (bool): 是否使用Azure AD认证,默认False

主要方法

  • test_connection(): 测试数据库连接
  • execute_query(query, params=None, fetch_all=True): 执行查询
  • execute_non_query(query, params=None): 执行非查询操作
  • get_connection(): 获取连接上下文管理器
  • get_database_tables(): 获取所有表名
  • get_table_info(table_name): 获取表结构信息

异常处理

包提供了以下自定义异常类:

  • AzureODBCError: 基础异常类
  • ConnectionError: 连接相关异常
  • QueryError: 查询执行异常
  • ConfigurationError: 配置相关异常
from azure_odbc_client import AzureODBCClient, ConnectionError, QueryError

try:
    client = AzureODBCClient(
        server="invalid-server",
        database="test",
        username="user",
        password="pass"
    )
    results = client.execute_query("SELECT * FROM users")
except ConnectionError as e:
    print(f"连接失败: {e}")
except QueryError as e:
    print(f"查询失败: {e}")

系统要求

  • Python 3.7+
  • ODBC Driver 17 for SQL Server(推荐)
  • 网络访问Azure SQL Server的权限

开发

安装开发依赖

pip install -e ".[dev]"

运行测试

pytest

代码格式化

black azure_odbc_client/

许可证

MIT License

贡献

欢迎提交Issue和Pull Request!

更新日志

1.0.0

  • 初始版本发布
  • 支持Azure SQL Server连接
  • 支持SQL Server和Azure AD认证
  • 提供基本的查询和操作功能

Metadata

Release files for azure-odbc-client 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for azure-odbc-client 1.0.0
File Size Uploaded Provenance
azure_odbc_client-1.0.0.tar.gz 10.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for azure-odbc-client 1.0.0
File Interpreter ABI Platform
azure_odbc_client-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / azure_odbc_client-1.0.0.tar.gz

Download URL azure_odbc_client-1.0.0.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
a32816ad4456da15fdd76ca0fb61012d6166faeb26b03684a8f70fb3fd13534b
BLAKE2b-256 checksum
How to use checksums
2b595f9e0b7d5efcc3976f5c29bd031426f635ab734476d13e3919440ec571f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Release files / azure_odbc_client-1.0.0-py3-none-any.whl

Download URL azure_odbc_client-1.0.0-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89532649c13bceee155e61a5d3a626a06ed4e0212a8ca02f353eb43872075ae9
BLAKE2b-256 checksum
How to use checksums
10364e1e76fc54ad19c9c4bb227fd76c8432818135f64c92fd5f777ea76833d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.7

Security for azure-odbc-client 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page