按日期对象python生成sql命令

按日期对象python生成sql命令,python,mysql,string,date,pyodbc,Python,Mysql,String,Date,Pyodbc,我想通过python编写mysql的“select”命令,我想使用python的date-time对象: cnxn = pyodbc.connect('DSN=aaa;') cursor = cnxn.cursor() cursor.execute("select * from tableA where time>" + str(CurrentTime)+ " order by time asc") 其中CurrentTime的类型为datetime.datetime 当我打印上面的字符

我想通过python编写mysql的“select”命令,我想使用python的date-time对象:

cnxn = pyodbc.connect('DSN=aaa;')
cursor = cnxn.cursor()
cursor.execute("select * from tableA where time>" + str(CurrentTime)+ " order by time asc")
其中CurrentTime的类型为
datetime.datetime

当我打印上面的字符串时,它看起来很好,但是当我运行这个时,它会给我一个错误:

pyodbc.ProgrammingError:('42000',“[42000][MySQL][ODBC 5.3(w) 驱动程序][mysqld-5.5.16-log]您的SQL语法有错误,请检查 右边是与MySQL服务器版本对应的手册 在第1行(1064)使用“11:17:07按时间排序asc”附近的语法 (SQLExecDirectW)“)

已解决: cursor.execute(““”从表A中选择*,其中时间>按时间排序asc”“”,str(CurrentTime))

使用参数:

cnxn = pyodbc.connect('DSN=aaa;')
cursor = cnxn.cursor()
cursor.execute("select * from tableA where time > ? order by time asc",
               CurrentTime)
我没有MySQL可供测试,所以可能我遗漏了一些东西