没有白走的路,每一步都算数🎈🎈🎈
预备知识:
需要准备的包:pymssql
import pymssql
连接数据库:
db = pymssql.connect(host='localhost', database='msdb', charset='utf8')
cursor = db.cursor()
变量初始化:
本文使用的是用三个英文字母拼接组成的变量名
为什么:因为这是目前能想到的既要满足数据库的表名不冲突,又能够创建很多数据库表的最简便的方法
A = [chr(i+65)for i in range(26)]
i = random.randint(0,10000)
j = random.randint(0,10000)
k = random.randint(0,10000)
table_name = A[i%26]+A[j%26]+A[k%26]
创建表:
sql = '''
CREATE TABLE %s(
Uname text NOT NULL,
content text NOT NULL,
ctime text NOT NULL,
)
''' % (table_name)
执行操作:
cursor.execute(sql)
db.commit()
完整代码:
import pymssql
db = pymssql.connect(host='localhost', database='msdb', charset='utf8')
cursor = db.cursor()
A = [chr(i+65)for i in range(26)]
i = random.randint(0,10000)
j = random.randint(0,10000)
k = random.randint(0,10000)
table_name = A[i%26]+A[j%26]+A[k%26]
oid = str(input("请输入视频的oid号:"))
sql = '''
CREATE TABLE %s(
Uname text NOT NULL,
content text NOT NULL,
ctime text NOT NULL,
)
''' % (table_name)
cursor.execute(sql)
db.commit()
效果图: