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!')

猜你喜欢

转载自www.cnblogs.com/airapple/p/9066067.html