使用python批量创建 mysql 表


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里面 , 通过循环拿出来创建.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM