使用 PyODBC 将参数传递给存储过程
我正在使用 pyODBC 连接到 SQL Server 2005 Express 数据库。我在 SQLServer Express 中创建了一个存储过程,它接受 2 个字符串参数,例如stored_proc(inpu1, input2),这些参数的类型为日期时间。我已经使用 Management Studio 测试了存储过程,它确实返回了适当的结果。但是,当我尝试从 python 调用存储过程(我正在使用 Eclipse)时,我收到错误。
pyodbc.DataError: ('22018', '[22018] [Microsoft][SQL Native Client]强制转换规范的字符值无效 (0) (SQLExecDirectW)')2/9/2011 12:00:03 2/ 9/2011 12:20:03
我正在调用的函数如下:
def GetAlarmFrequencyTime(tstart,tend):
print tstart, tend
arguments=(tstart,tend)
local_cursor=conn.cursor()
local_cursor.execute("{call dbo.GetAlarmEventHistoryFrequency_TimeRange(tstart,tend)}")
resultset_rows=local_cursor.fetchall()
print "There are" , len(resultset_rows), "records"
for single_row in resultset_rows:
print "|" ,single_row[0], "|" ,single_row[1],"|"
local_cursor.close()
导致问题的行是
local_cursor.execute("{call dbo.GetAlarmEventHistoryFrequency_TimeRange(tstart,tend)}")
任何人都可以帮助我了解如何将多个参数传递给使用 pyODBC 调用的存储过程。 我是否需要转换 tstart,并首先倾向于日期时间格式,如果需要如何?我已经验证了 strptime,但即使这样也没有成功,尽管我可能使用错误
I'm using pyODBC to connect to a SQL Server 2005 express database. I've created a stored procedure in SQLServer express that takes 2 string parameters e.g stored_proc(inpu1, input2) these parameters are of type datetime. I have tested the stored proc using management studio and it does return an appropriate result. However when i try to call the stored procedure from python(i'm using Eclipse) I get the error.
pyodbc.DataError: ('22018', '[22018] [Microsoft][SQL Native Client]Invalid character value for cast specification (0) (SQLExecDirectW)')2/9/2011 12:00:03 2/9/2011 12:20:03
The function i'm calling is as follows:
def GetAlarmFrequencyTime(tstart,tend):
print tstart, tend
arguments=(tstart,tend)
local_cursor=conn.cursor()
local_cursor.execute("{call dbo.GetAlarmEventHistoryFrequency_TimeRange(tstart,tend)}")
resultset_rows=local_cursor.fetchall()
print "There are" , len(resultset_rows), "records"
for single_row in resultset_rows:
print "|" ,single_row[0], "|" ,single_row[1],"|"
local_cursor.close()
The line that's causing the trouble is
local_cursor.execute("{call dbo.GetAlarmEventHistoryFrequency_TimeRange(tstart,tend)}")
can anyone help me understand how I can pass multiple parameters to a stored procedure called using pyODBC.
Do I need to convert tstart, and tend to a datetime format first, if so how? I have trued strptime, but even that's not succeeded, though i may be using it wrong
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我终于明白了,可以正常工作而不会崩溃
是错误的语法。应该是
特别要注意的是特殊符号{}以及参数传入的方式(tstart),(倾向) local_cursor.execute("*{*call dbo.GetAlarmEventHistoryFrequency_TimeRange*(?,?)}",(tstart),(tend) *)
I finally got it, to work without crashing
is the wrong syntax. It should be
Special attention should be made to the special symbols {} and the way the parameters are passed in (tstart),(tend) local_cursor.execute("*{*call dbo.GetAlarmEventHistoryFrequency_TimeRange*(?,?)}",(tstart),(tend)*)