python之操作数据库删除创建
import MySQLdb # 创建一个数据库连接对象 conn = MySQLdb.connect( host='localhost', port=3306, user='root', password='123456' ) # 获取连接的游标 cursor = conn.cursor() # 执行语句,创建数据库TestDB cursor.execute('create database TestDB') cursor.execute('drop database TestDB') # 执行语句,展示所有数据库 cursor.execute('show databases') # 遍历所有数据库 (多维元组) for i in cursor: print(i) # 关闭游标 cursor.close() # 关闭连接 conn.close()
-----------------------------------------------------------------------------------------------------------------------------------------