You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

import random
import sqlite3
import uuid
def getConn():
conn = sqlite3.connect('../HuiYa.db')
return conn
def generate_random_pair(user_id, model_id, style_count, pose_count):
for i in range(1, style_count * pose_count + 1):
# 生成随机数
style_id = random.randint(1, style_count)
pose_id = random.randint(1, pose_count)
# 连接到SQLite数据库
# 如果文件不存在,会自动在当前目录创建一个数据库文件
cursor.execute(
"SELECT * FROM t_user_style_pose where user_id=? and model_id=? and style_id=? and pose_id=?",
(user_id, model_id, style_id, pose_id))
exists = cursor.fetchone()
if exists is None:
# 如果这个组合不存在,返回这个组合
return style_id, pose_id
def write_random_pair(user_id, model_id, style_id, pose_id):
cursor.execute("INSERT INTO t_user_style_pose (user_id,model_id,style_id,pose_id) VALUES (?, ?,?,?)",
(user_id, model_id, style_id, pose_id))
conn.commit()
def query(sql):
cursor.execute(sql)
return cursor.fetchall()
# Sqlite3初始化
conn = getConn()
cursor = conn.cursor()
# 插入初始数据(如果需要)
user_id = str(uuid.uuid4())
style_count = 5
pose_count = 5
model_id = 1
for i in range(1, 11):
style_id, pose_id = generate_random_pair(user_id, model_id, 5, 5)
write_random_pair(user_id, model_id, style_id, pose_id)
print(query("select * from t_user_style_pose"))
# 关闭数据库
conn.close()