Python 中的数据库 DELETE 操作
当你要从数据库中删除一些记录时,需要 DELETE 操作。以下是在 EMPLOYEE 中删除 AGE 大于 20 的所有记录的过程 −
示例
#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to DELETE required records sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
广告