sqlQu = 'SELECT id,university_id,grade FROM a_kuakao_school WHERE university_id > 0 ORDER BY university_id ASC' self.cursor.execute(sqlQu) result = self.cursor.fetchall() for row in result: if row[4]: grade = '"%s"' % row[2] else: grade = 'NULL' sqlUp = 'UPDATE university SET grade = %s WHERE id = %d' % (grade,row[1]) print(sqlUp) self.cursor.execute(sqlUp) self.cursor.connection.commit()
python在mysql中插入null空值应该跟上边的代码一样。
注意:语句grade = %s不加引号(重点)改在上边判断如果grade = "%s"一旦加上引号之后语句就是UPDATE university SET grade = "NULL" WHERE id = 1
引号不加的话字符串肯定是更新或者插入不进去的,这样
if row[4]:
grade = '"%s"' % row[4]
else:
grade = 'NULL'
操作就可以了更新同理。