之前有壹個地方寫的
cursor.execute("select * from table1 where name = %s", name)
用法沒問題,而在最近壹次寫腳本的時候,這樣寫
cursor.execute("delete from %s where data_date='2017-11-29'", table2)
這個時候報錯了
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version……
修改成
cursor.execute("delete from %s where data_date='2017-11-29'" % table2)
或者
cursor.execute("delete from {} where data_date='2017-11-29'".format(table2))
就沒問題了。但是對之前這樣用沒報錯感到了疑問……查了資料後得知
在pymysql的execute方法中,執行的mysql語句中用%s替換的參數外加上了單引號。而在mysql的語句中表名和列名外都不能加單引號,而值則可以加單引號。
這下明白了……