假设有一个数据库表,字段如下:
CREATE TABLE direct_cost (
pkid INT AUTO_INCREMENT PRIMARY KEY,
rate_type VARCHAR(50),
depart CHAR(1),
budget FLOAT,
INDEX idx_depart (depart)
);
通过 python3 脚本给这个表构造数据:
import random
budget_values = [random.uniform(0.5, 200.0) for _ in range(100)]
rate_types = ['预算执行率', '收入指标完成率']
departs = ['A', 'B', 'C']
sql_statements = []
for i in range(100):
sql_statements.append(f"('{random.choice(rate_types)}', '{random.choice(departs)}', {budget_values[i]})")
# 将 INSERT 语句的前导部分与生成的 VALUES 列表组合起来
base_insert = "INSERT INTO direct_cost (rate_type, depart, budget) VALUES "
full_insert = base_insert + ",\n".join(sql_statements) + ";"
# 打印或执行 SQL 语句
print(full_insert)