当前位置:主页 > 软件编程 > Python代码 >

利用Python脚本批量生成SQL语句

时间:2021-03-06 10:14:15 | 栏目:Python代码 | 点击:

通过Python脚本批量生成插入数据的SQL语句

原始SQL语句:

INSERT INTO system_user (id, login_name, name, password, salt, code, createtime, email, main_org, positions, status, used, url, invalid, millis, id_card, phone_no, past, end_date, start_date) 
VALUES ('6', 'db', 'db', '53dd4e491d16f21b19606e8fb0619522e6d5f307', 'a211f9dd3120178a', NULL, sysdate, '1@springside.org.cn', NULL, '', 'enabled', 'Used', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

CreateSQL.py

f = open("/Users/apple/Downloads/sql/insertUser.sql",'w')

for i in range(6,57):
 str_i = str(i)
 login_name = "test"+str_i
 name = "test"+str_i
 sql = 'INSERT INTO system_user (id, login_name, name, password, salt, code, createtime, email, main_org, positions, status, used, url, invalid, millis, id_card, phone_no, past, end_date, start_date) ' \
   'VALUES ("'+str_i+'","'+login_name+'","'+name+'","53dd4e491d16f21b19606e8fb0619522e6d5f307", "a211f9dd3120178a", NULL, sysdate, "1@springside.org.cn", NULL, "''", "enabled", "Used", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);'
 f.write(sql)
 f.write("\n")

print('50 success!')
f.close()

通过脚本拼接出一条插入数据的SQL,然后通过For循环生成批量SQL语句

生成结果:

您可能感兴趣的文章:

相关文章