Home >Backend Development >Python Tutorial >mysql database learning additional mysql delete all tables under the database
In the previous three articles, the creation and use of Python mysql database was introduced in detail, but in actual problems, it is also necessary to mysql delete all lists under the library , this article can be regarded as an additional article to the previous three articles to introduce mysql to delete all lists under the library.
Similar to Deleting all lists under the library, we also need to learn how to update the database
Used for update operations To update the data in the data table, the following example increments the AGE field whose SEX field is 'M' in the EMPLOYEE table by 1:
#!/usr/bin/python# -*- coding: UTF-8 -*- import MySQLdb # 打开数据库连接 db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 更新语句 sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'" % ('M') try: # 执行SQL语句 cursor.execute(sql) # 提交到数据库执行 db.commit() except: # 发生错误时回滚 db.rollback() # 关闭数据库连接 db.close()
Delete operation
The delete operation is used to delete data in the data table. The following example demonstrates deleting all data with an AGE greater than 20 in the data table EMPLOYEE
#!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb # 打开数据库连接 db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB", charset='utf8' ) # 使用cursor()方法获取操作游标 cursor = db.cursor() # SQL 删除语句 sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20) try: # 执行SQL语句 cursor.execute(sql) # 提交修改 db.commit() except: # 发生错误时回滚 db.rollback() # 关闭连接 db.close(
The above is the detailed content of mysql database learning additional mysql delete all tables under the database. For more information, please follow other related articles on the PHP Chinese website!