python中用None表示空,但是在查询Mysql数据中,参数不能传None
例如
def select_word1(): db = MysqlConn() cursor = db.cursor() sql = 'select * from HotWord where detail=%s' try: cursor.execute(sql,None) row = cursor.fetchone() print(row) except: traceback.print_exc() print('查询失败') db.rollback() db.close() if __name__ == '__main__': select_word1()
会报错
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
正确用法
def delete_None_detail(): db = MysqlConn() cursor = db.cursor() sql = 'delete from HotWord where detail is null' try: cursor.execute(sql) db.commit() print('删除成功') except: traceback.print_exc() print('删除失败') db.rollback() db.close()
Mysql中用 is null表示空