1、创建1000张表
import pymysql
dbhost = "10.1.1.143"
dbuser = "root"
dbpassword = "123456"
dbname = "demo_cg1000"
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
2、创建1张有50个+字段的表
import pymysql
dbhost = "10.1.1.143"
dbuser = "root"
dbpassword = "123456"
dbname = "demo_cg1000"
dbport = 3306
dbconn = pymysql.connect(host=dbhost, user=dbuser, password=dbpassword, db=dbname, port=dbport)
mycursor = dbconn.cursor()
fields = []
for i in range(1, 60):
fields.append(f'a{i} VARCHAR(255)')
create_table_sql = f'CREATE TABLE mytable11 ({", ".join(fields)});'
print(create_table_sql)
mycursor.execute(create_table_sql)
dbconn.close
创建1000个60个字段的表
import pymysql
dbhost = "10.1.1.143"
dbuser = "root"
dbpassword = "123456"
dbname = "demo_cg1000"
dbport = 3306
dbconn = pymysql.connect(host=dbhost, user=dbuser, password=dbpassword, db=dbname, port=dbport)
mycursor = dbconn.cursor()
for num in range(1, 1001):
tablename = "user" + str(num)
sql = "CREATE TABLE %s (a1 VARCHAR(255), a2 VARCHAR(255), a3 VARCHAR(255), a4 VARCHAR(255), a5 VARCHAR(255), a6 VARCHAR(255), a7 VARCHAR(255), a8 VARCHAR(255), a9 VARCHAR(255), a10 VARCHAR(255), a11 VARCHAR(255), a12 VARCHAR(255), a13 VARCHAR(255), a14 VARCHAR(255), a15 VARCHAR(255), a16 VARCHAR(255), a17 VARCHAR(255), a18 VARCHAR(255), a19 VARCHAR(255), a20 VARCHAR(255), a21 VARCHAR(255), a22 VARCHAR(255), a23 VARCHAR(255), a24 VARCHAR(255), a25 VARCHAR(255), a26 VARCHAR(255), a27 VARCHAR(255), a28 VARCHAR(255), a29 VARCHAR(255), a30 VARCHAR(255), a31 VARCHAR(255), a32 VARCHAR(255), a33 VARCHAR(255), a34 VARCHAR(255), a35 VARCHAR(255), a36 VARCHAR(255), a37 VARCHAR(255), a38 VARCHAR(255), a39 VARCHAR(255), a40 VARCHAR(255), a41 VARCHAR(255), a42 VARCHAR(255), a43 VARCHAR(255), a44 VARCHAR(255), a45 VARCHAR(255), a46 VARCHAR(255), a47 VARCHAR(255), a48 VARCHAR(255), a49 VARCHAR(255), a50 VARCHAR(255), a51 VARCHAR(255), a52 VARCHAR(255), a53 VARCHAR(255), a54 VARCHAR(255), a55 VARCHAR(255), a56 VARCHAR(255), a57 VARCHAR(255), a58 VARCHAR(255), a59 VARCHAR(255), a60 VARCHAR(255))" % tablename
#print(sql)
mycursor.execute(sql)
dbconn.close