Python:给MySQL创建1000张表和创建1张有50个字段的表

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

你可能感兴趣的:(python相关,#,MySQL数据库,python,服务器)