import MySQLdb # 打開數據庫連接 這是虛擬機映射為網絡數據庫 db = MySQLdb.connect("192.168.1.11", "hive", "hive", "yewu001", port=3306, charset='utf8') #---------------- IP 用戶名 密碼 database 端口port 字符編碼 # 使用cursor()方法獲取操作游標 cursor = db.cursor() # 使用execute方法執行SQL語句 for i in range(202001,202013): sql="create table table%s(id Int(5),name VARCHAR(16))"%i cursor.execute(sql) db.commit() cursor.execute("show tables;") # 使用 fetchall() 方法獲取返回結果數據集 data = cursor.fetchall() for d in data: print(d) # 關閉數據庫連接 db.close()

總結:使用循環 替換表名 , 這里創建了12個表 . 沒什么難度,當然 , 也可以把表名放到一個list里面 , 通過循環拿出來創建.