Passing parameters not being recognized and throws SQL error when executing raw query (on SQL-Server database and Pymssql) with SqlAlchemy(使用SqlAlChemy执行原始查询(针对SQL-Server数据库和Pymssql)时,传递无法识别的参数并引发SQL错误)
问题描述
我正在尝试使用SqlAlChemy(将Pymssql作为提供程序)在SQL-Server数据库上执行简单的原始SQL查询。这是我的第一次尝试(使用execute连接方法并以**kwargs方式传递参数):
provider = DataProvider().engine
q = "select url from Crawler.CrawlSource where source=@source"
query = text(q)
result = provider.connect().execute(query, source ='mysource')
我以教程中所示的任何方式传递参数(作为kwargs传递和作为dict传递),但它们都不起作用,并且当调用execute方法时抛出异常,该异常显示'Must declare the scalar variable' @source好像没有参数传递给execute方法,看起来ORM(或者可能是数据提供程序(本例中为pymssql))不识别传递给execute方法的参数,只传递query(
我猜MSSQL-Server提供程序(Pymssql)可能有问题,因为SQL-Server在SqlAlChemy和Python家族中不是一等公民,但没有直接线索导致此问题。
如上所述,我还尝试了其他方法
这是我的第二次尝试(使用execute连接方法并将参数作为dict传递):
provider = DataProvider().engine
q = "select url from Crawler.CrawlSource where source=@source"
query = text(q)
result = provider.connect().execute(query, {source :'mysource'})
我的第三次尝试(使用Engine对象的execute方法,以**kwargs方式传递参数):
provider = DataProvider().engine
q = "select url from Crawler.CrawlSource where source=@source"
query = text(q)
result = provider.execute(query, source ='mysource')
我的第四次尝试(使用Engine对象的execute方法,参数作为dict传递):
provider = DataProvider().engine
q = "select url from Crawler.CrawlSource where source=@source"
query = text(q)
result = provider.execute(query, {source :'mysource'})
我的第五次尝试(创建Session并使用Session的execute方法,将参数作为dict传递):
provider = DataProvider().engine
session = sessionmaker(bind=provider)()
q = "select url from Crawler.CrawlSource where source=@source"
query = text(q)
result = session.execute(query, {source :'mysource'})
我的第六次尝试(创建Session,使用Session的execute方法,以**kwargs方式传递参数):
provider = DataProvider().engine
session = sessionmaker(bind=provider)()
q = "select url from Crawler.CrawlSource where source=@source"
query = text(q)
result = session.execute(query, source='mysource')
但正如我前面提到的,上述努力都没有奏效,它们都导致了上面提到的相同异常
如有任何帮助,我们将不胜感激
推荐答案
mssql+pymssql方言似乎支持";pyformat";paramstyle。这对我有效:
import sqlalchemy as sa
engine = sa.create_engine("mssql+pymssql://@localhost:49242/myDb")
sql = "SELECT word FROM vocabulary WHERE language = %(lang)s"
params = {'lang': 'Greek'}
with engine.begin() as conn:
result = conn.execute(sql, params).fetchall()
print(result)
# [('γιορτή',), ('ηλεκτρονικός υπολογιστής',)]
如果使用SQLAlChemytext对象,还可以使用名为";的参数样式:
sql = sa.sql.text("SELECT word FROM vocabulary WHERE language = :lang")
params = {'lang': 'Greek'}
with engine.begin() as conn:
result = conn.execute(sql, params).fetchall()
print(result)
# [('γιορτή',), ('ηλεκτρονικός υπολογιστής',)]
text对象允许我们一致使用名为";的参数样式,而不考虑DB-API层支持的本机参数样式(例如,%s用于pymssql,?用于pyodbc)。
这篇关于使用SqlAlChemy执行原始查询(针对SQL-Server数据库和Pymssql)时,传递无法识别的参数并引发SQL错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用SqlAlChemy执行原始查询(针对SQL-Server数据库和Pymssql)时,传递无法识别的参数并引发SQL错误
基础教程推荐
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
