十几行代码将mock生成的json数据转为sql的insert语句
事情是这样的,我之前发的前端代码,调用接口的时候不是用mock.js生成的数据吗?然后最近学习用SpringBoot给它安排个后端,于是不可避免的就要创建数据库查表了!但是,没有数据怎么办??我灵机一动,想到了现在的方法,将mock生成的json数据,逆向转换成sql的insert语句,直接往表里插入~
@
mock生成的数据格式
{
"code": 2000,
"flag": true,
"message": "查询成功",
"data": {
"total": 800,
"rows": [
{
"id": 1,
"cardNum": 4034699513216515,
"name": "陈秀英",
"birthday": "2001-01-10",
"phone": "03149678832",
"integral": 419,
"money": 448.9,
"payType": "3",
"address": "澳门特别行政区 离岛 -"
},
{
"id": 2,
"cardNum": 8858101787577042,
"name": "侯磊",
"birthday": "1985-05-15",
"phone": "16376731716",
"integral": 188,
"money": 76.932,
"payType": "3",
"address": "浙江省 杭州市 其它区"
},
{
"id": 3,
"cardNum": 8825696112402666,
"name": "梁强",
"birthday": "1980-05-29",
"phone": "36953709502",
"integral": 97,
"money": 833.08,
"payType": "3",
"address": "湖北省 黄冈市 黄梅县"
},
{
"id": 4,
"cardNum": 5564348763188378,
"name": "锺明",
"birthday": "2009-08-16",
"phone": "79463715626",
"integral": 87,
"money": 99.51,
"payType": "3",
"address": "西藏自治区 阿里地区 札达县"
},
{
"id": 5,
"cardNum": 1791603690156761,
"name": "孔勇",
"birthday": "2006-05-16",
"phone": "85285286149",
"integral": 330,
"money": 441.018,
"payType": "2",
"address": "湖北省 咸宁市 通山县"
},
{
"id": 6,
"cardNum": 968224456159813,
"name": "姚刚",
"birthday": "1985-09-30",
"phone": "74659780924",
"integral": 126,
"money": 383.757,
"payType": "2",
"address": "澳门特别行政区 澳门半岛 -"
},
{
"id": 7,
"cardNum": 2618032223395428,
"name": "冯磊",
"birthday": "2012-09-12",
"phone": "18633322503",
"integral": 237,
"money": 33.69,
"payType": "4",
"address": "内蒙古自治区 锡林郭勒盟 东乌珠穆沁旗"
},
{
"id": 8,
"cardNum": 2290756015656033,
"name": "锺洋",
"birthday": "1994-02-17",
"phone": "59354833946",
"integral": 384,
"money": 684.2,
"payType": "2",
"address": "新疆维吾尔自治区 克拉玛依市 克拉玛依区"
},
{
"id": 9,
"cardNum": 176069141058197,
"name": "董霞",
"birthday": "2018-10-22",
"phone": "19633855668",
"integral": 218,
"money": 823.8,
"payType": "2",
"address": "贵州省 黔南布依族苗族自治州 龙里县"
},
{
"id": 10,
"cardNum": 7876319942192848,
"name": "范芳",
"birthday": "1998-10-18",
"phone": "72964427183",
"integral": 172,
"money": 469.14,
"payType": "3",
"address": "安徽省 马鞍山市 雨山区"
}
]
}
}
这里我们只取数据部分就行了,也就是rows[]中的数据(一个个的{"id":xx,"cardNum":xx,...}),复制到一个json文件中
Python代码实现转换
'''
json文件要是gbk编码,生成的sql文件好像也是gbk的,不能直接导入。。
需要替换字符串s的初始sql,在navcat的表上右键复制insert方法,然后把VALUES后边的全删了就行了。
如果需要同时设置id,则把withId改为True
没有自动装配,请确保json数据和表的字段顺序一致!
'''
withId = False
with open("member.json", "r")as f:
json=f.read()
s=f"INSERT INTO `mxg_member`.`tb_member` (`id`, `card_num`, `name`, `birthday`, `phone`, `integral`, `money`, `pay_type`, `address`) VALUES "
if not withId:
s = s.split('id')[0][:-1] + s.split('id')[1][2:]
s += "\n"
json = json.split("\n")
for i in json:
if "id" in i:
s+=f"({i.split(':')[1] if withId else ''}"
elif ":" in i:
s+=i.split(":")[1]
elif "}" in i:
s+="),\n"
with open("member.sql", "w")as f:
f.write(s[:-2]+";")
这里还做了判断是否需要id,因为一般id字段是自增的,所以默认是不需要id,如果需要id的话,将withId改为
True
即可(区分大小写)注意:表的字段要和mock生成的数据一一对应(名称不一样可以,但顺序要一样。)建议看情况修改字符串
s
的insert语句的字段顺序。
转换结果
插入到数据库,成功了哦!
Postman也能访问到数据了~
一共361条数据(还有我之前插入的),每页显示20个,查第19页,只能返回最后一个陈强,也就是刚刚插入的最后一个会员信息。