1、首先需要安装pymysql
2、连接数据库,并使用for循环,拼接表名和sql语句
代码如下:
import pymysql
dbhost = "10.1.1.143"
dbuser = "root"
dbpassword = "lianshi2020"
dbname = "table120"
dbport = 3306
dbconn = pymysql.connect(host=dbhost, user=dbuser, password=dbpassword, db=dbname,port=dbport)
mycursor = dbconn.cursor()
for num in range(1, 1001):
#print (num)
tablename = "user" + str(num)
sql = "CREATE TABLE %s (col1 INT PRIMARY KEY NOT NULL,col2 varchar(300) DEFAULT NULL,col3 varchar(300) DEFAULT NULL,col4 varchar(300) DEFAULT NULL,col5 varchar(300) DEFAULT NULL,col6 varchar(300) DEFAULT NULL,col7 varchar(300) DEFAULT NULL,col8 varchar(300) DEFAULT NULL,col9 varchar(300) DEFAULT NULL,col10 varchar(300) DEFAULT NULL)" % tablename
mycursor.execute(sql)
dbconn.close
|