文章目录
- 一、获取Flask、SQLAlchemy实例,绑定Flask实例
- 二、插入csv文件中的数据
- 三、插入json中的数据
一、获取Flask、SQLAlchemy实例,绑定Flask实例
db = SQLAlchemy()
db.init_app(app)
其中app来自工厂函数
def create_app(app):app = Flask(__name__)app.config.from_object(Config)config_extensions(app)return app
二、插入csv文件中的数据
数据文件:mysql.csv(h3c套一提供数据)
1.声明orm关系映射
from app.models import dbclass Tbl_hotel_test(db.Model):__tablename__ = 'tbl_hotel_test'seq = db.Column(db.String(30),primary_key=True) hotel_name = db.Column(db.String(100)) country = db.Column(db.String(50)) province = db.Column(db.String(50)) city = db.Column(db.String(50)) business_district = db.Column(db.String(100)) start = db.Column(db.String(30)) department = db.Column(db.String(50)) rooms = db.Column(db.Integer) photos = db.Column(db.Integer) score = db.Column(db.DECIMAL(15,12)) content = db.Column(db.Integer) city_avg_night = db.Column(db.DECIMAL(6,2)) hotel_total_order = db.Column(db.Integer) hotel_total_night = db.Column(db.Integer) hotel_right_order = db.Column(db.Integer) hotel_right_night = db.Column(db.Integer) hotel_direct_order = db.Column(db.Integer) hotel_direct_night = db.Column(db.Integer) hotel_direct_right_order = db.Column(db.Integer) hotel_direct_right_night = db.Column(db.Integer) hotel_direct_refuse_order = db.Column(db.Integer) hotel_direct_refuse_pie = db.Column(db.String(20)) city_direct_order = db.Column(db.Integer) city_direct_Refuse_pie = db.Column(db.String(20)) directcity_issmall = db.Column(db.Integer)
2.插入数据
@main_print.route('/addData')
def main_add():print('开始插入')with open('app/static/data/mysql.csv','r',encoding='utf-8') as f:reader = csv.reader(f)if reader != None:for i in reader:hotel = Tbl_hotel_test(seq=i[0],hotel_name=i[1],country=i[2],province=i[3],city=i[4],business_district=i[5],start=i[6],department=i[7],rooms=i[8],photos=i[9],score=i[10],content=i[11],city_avg_night=i[12],hotel_total_order=i[13],hotel_total_night=i[14],hotel_right_order=i[15],hotel_right_night=i[16],hotel_direct_order=i[17],hotel_direct_night=i[18],hotel_direct_right_order=i[19],hotel_direct_right_night=i[20],hotel_direct_refuse_order=i[21],hotel_direct_refuse_pie=i[22],city_direct_order=i[23],city_direct_Refuse_pie=i[24],directcity_issmall=i[25])db.session.add(hotel)db.session.commit()return render_template('/test/sucess.html',content='插入成功')return ''
3. 效果
三、插入json中的数据
1.ORM关系映射
from app.models import db'''
城市,公司全名,公司ID,公司简称,公司大小,公司所在商区,职位所属 ,教育要求,公司所属领域 ,职位ID,职位福利,职位名称,薪水,工作年限要求
'''class Tbl_zp(db.Model):__tablename__ = 'tbl_zp_test'id = db.Column(db.Integer,primary_key=True,autoincrement=True) company_id = db.Column(db.Integer) city = db.Column(db.String(20)) conpany_name = db.Column(db.String(50)) conpany_simple_name = db.Column(db.String(20)) conpany_size = db.Column(db.String(20)) conpany_area = db.Column(db.String(50)) job_category = db.Column(db.String(20)) school_level = db.Column(db.String(10)) conpany_category = db.Column(db.String(20)) job_id = db.Column(db.String(20)) job_get = db.Column(db.String(200)) job_name = db.Column(db.String(50)) job_salary = db.Column(db.String(20)) job_year = db.Column(db.String(50))
2.插入数据
@main_print.route('/addData2')
def main_add2():print('开始插入')result = []with open('app/static/data/zpdata.json',encoding='utf-8') as f:result.append(json.load(f))if result != None:for r in result:for i in r:'''城市,公司全名,公司ID,公司简称,公司大小,公司所在商区,职位所属 ,教育要求,公司所属领域 ,职位ID,职位福利,职位名称,薪水,工作年限要求'''try:zp = Tbl_zp(city=i['城市'], conpany_name=i['公司全名'], company_id=i['公司ID'],conpany_simple_name=i['公司简称'], conpany_size=i['公司大小'], conpany_area=i['公司所在商区'], job_category=i['职位所属'],school_level=i['教育要求'], conpany_category=i['公司所属领域'], job_id=i['职位ID'],job_get=i['职位福利'],job_name=i['职位名称'], job_salary=i['薪水'], job_year=i['工作年限要求'])db.session.add(zp)except:db.session.delete(zp)db.session.commit()return render_template('/test/sucess.html', content='插入成功')return ''
3.效果