2014-06-13 146 views
1

使SQL命令我想寫通過蟒蛇的mysql的「選擇」命令,我想使用Python的日期時間對象:按日期對象蟒蛇

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) Driver][mysqld-5.5.16-log]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '11:17:07 order by time asc' at line 1 (1064) (SQLExecDirectW)")

解決: cursor.execute(「」「SELECT * FROM表A,時間?>爲了按時間遞增 「」」,STR(CURRENTTIME))

回答

2

使用參數:

cnxn = pyodbc.connect('DSN=aaa;') 
cursor = cnxn.cursor() 
cursor.execute("select * from tableA where time > ? order by time asc", 
       CurrentTime) 

我沒有與MySQL的測試,所以也許我失去了一些東西。