json数据生成mySQL语句
Posted Cyan的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json数据生成mySQL语句相关的知识,希望对你有一定的参考价值。
import json,codecs
def readjson(file):
with open(file,‘rb‘) as fp:
data = json.load(fp)
return data
list1 = []
loadjson = readjson(‘market.json‘)
profile = loadjson[‘data‘][‘products‘]
str1 = ‘insert into products (id ,name,product_id,long_name,store_nums,specifics,sort,market_price,price,‘ \
‘category_id,child_cid,img,keywords,brand_id,brand_name,safe_day,safe_unit,safe_unit_desc)values‘
for k,v in profile.items():
for product in v:
str2 = ‘‘
str2 += ‘("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")‘\
%(str(product[‘id‘]),str(product[‘name‘]),str(product[‘product_id‘]),str(product[‘long_name‘]),str(product[‘store_nums‘]),
str(product[‘specifics‘]),str(product[‘sort‘]),str(product[‘market_price‘]),str(product[‘price‘]),str(product[‘category_id‘]),
str(product[‘child_cid‘]),str(product[‘img‘]),str(product[‘keywords‘]),str(product[‘brand_id‘]),str(product[‘brand_name‘]),
str(product[‘safe_day‘]),str(product[‘safe_unit‘]),str(product[‘safe_unit_desc‘]))
list1.append(str2)
str3 = str1+‘,‘.join(list1)+‘;‘
print(str3)
file = codecs.open(‘market.sql‘,‘wb‘,‘utf-8‘)
file.write(str3)
file.close()
print(‘ok!‘)
以上是关于json数据生成mySQL语句的主要内容,如果未能解决你的问题,请参考以下文章
Python将JSON格式数据转换为SQL语句以便导入MySQL数据库
powerdesigner如何生成java代码跟mysql语句?
MySQL数据同步到HIVE,自动生成MySQL表对应HIVE的建表语句