当前位置:首页 >> 脚本专栏

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

通过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语句

生成结果:

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

以上这篇利用Python脚本批量生成SQL语句就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。