json 脚本入库的几种方法
Posted Xcsg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json 脚本入库的几种方法相关的知识,希望对你有一定的参考价值。
json 脚本入库的几种方法,见代码:
#-*- encoding: utf-8 -*-
#第一种mongodb入库
# from pymongo import *
# import json
# conn = MongoClient(‘127.0.0.1‘,27017)
# db = conn.p2p
# content = open(‘static/data/text.json‘,‘r‘,encoding=‘utf-8‘,errors=‘ignore‘)
# data = json.load(content)
# db.order.insert(data)
# print(db.order.count())
# 第二种SQLALCHEMY入库mysql
# import json
# from flask import Flask
# from flask_sqlalchemy import SQLAlchemy
# app = Flask(__name__)
# app.config.from_pyfile("config.ini")
# db = SQLAlchemy(app)
# class Product(db.Model):
# __tablename__= ‘product‘
# id = db.Column(db.Integer,primary_key=True)
# name = db.Column(db.String(200))
# price = db.Column(db.Integer)
# @app.route("/")
# def index():
# content = open(‘static/data/test.json‘,‘r‘,encoding=‘utf-8‘,errors=‘ignore‘)
# data = json.load(content)
# for i in data:
# ret = Product(name=i[‘name‘],price = i[‘price‘])
# db.session.add(ret)
# return "入库成功"
# if __name__ == "__main__":
# app.run()
#第三种 原生sql 入库mysql
# import json
# from pymysql import *
# conn = connect(host="127.0.0.1",port=3306,user=‘root‘,password=‘mysql‘,database="myflask",charset="utf8")
# cur = conn.cursor()
# content = open(‘static/data/text.json‘,‘r‘,encoding=‘utf-8‘,errors=‘ignore‘)
# data = json.load(content)
# for i in data:
# cur.execute("insert into order values(0,‘"+i[‘name‘]+"‘,‘"+i[‘price‘]+"‘)")
# conn.commit
以上是关于json 脚本入库的几种方法的主要内容,如果未能解决你的问题,请参考以下文章