且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 sqlalchemy 和 pyodbc 连接到 SQL Server 2012

更新时间:2023-02-03 18:56:39

SQLAlchemy 将基于文件的 DSN 字符串解释为 server name = c, database name = users代码>.

The file-based DSN string is being interpreted by SQLAlchemy as server name = c, database name = users.

我更喜欢在不使用 DSN 的情况下进行连接,这是在代码迁移期间要处理的配置任务少.

I prefer connecting without using DSNs, it's one less configuration task to deal with during code migrations.

此语法适用于 Windows 身份验证:

This syntax works using Windows Authentication:

engine = sa.create_engine('mssql+pyodbc://server/database')

或者使用 SQL 身份验证:

Or with SQL Authentication:

engine = sa.create_engine('mssql+pyodbc://user:password@server/database')

SQLAlchemy 对不同的连接字符串选项有详尽的解释 此处.

SQLAlchemy has a thorough explanation of the different connection string options here.