1
Python3.5 SQL服务器2012标准如何日期变量传递给一个SQL查询在Python
包pypyodbc
此代码的工作
myConnection = pypyodbc.connect('Driver={SQL Server};'
'Server=myserver;'
'Database=mydatabase;'
'TrustedConnection=yes')
myCursor = myConnection.cursor()
sqlstr = ("Select * From DB.Table1 Where DB.Table1.Date <= '7/21/2016'")
myCursor.execute(sqlstr)
results = myCursor.fetchall()
但是,日期必须是变量由用户传入。我做了几个MODS的sqlstr但继续在myCursor.execute错误: “类型错误:字节或整数地址预期,而不是元组实例”
sqlstr = ("Select * From DB.Table1 Where DB.Table1.Date <= %s", '7/21/2016')
错误
sqlstr = ("Select * From DB.Table1 Where DB.Table1.Date <= '%s'", '7/21/2016')
错误
sqlstr = ("Select * From DB.Table1 Where DB.Table1.Date <= ?", "'7/21/2016'")
错误
var1 = "'7/21/2016'"
sqlstr = ("Select * From DB.Table1 Where DB.Table1.Date <= %s", var1)
等等。不过,我确信有一个正确的方法...
感谢您的任何帮助!
谢谢你,那确实起作用。对于对多个参数感兴趣的人,只需用逗号分隔[param1,param2,param3]即可。另外,我不必编辑整个查询以进行参数化,只需要将变量包含在括号中。 – pbean