从txt文件初始化Flask数据库

时间:2018-09-16 10:02:41

标签: flask sqlalchemy flask-sqlalchemy

我尝试将txt文件中的所有电影标题作为项目添加到我的sqlalchemy数据库中。

但是似乎可以使用常用方法(如 @SuppressLint({"SetTextI18n",}) public void onClick(View v) { LinearLayout ll = new LinearLayout(getApplicationContext()); ll.setOrientation(LinearLayout.HORIZONTAL); ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100)); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.weight = 3.0f; params.setMargins(10,0,10,0); // ll.setPadding(5, 1, 10, 0); final TextView txt = new TextView(getApplicationContext()); final EditText txtLoad = new EditText(getApplicationContext()); final EditText txtLoad1 = new EditText(getApplicationContext()); final EditText txtLoad2 = new EditText(getApplicationContext()); final EditText txtLoad3 = new EditText(getApplicationContext()); txt.setText( ""+(j + 1)); txt.setPadding(5, 0, 5, 0); txt.setTextColor(Color.BLACK); txt.setTextSize(20); txtLoad.setHintTextColor(Color.GRAY); txtLoad.setHint("Empty"); txtLoad.setPadding(5, 0, 1, 0); int txtloadid = j+10; txtLoad.setLayoutParams(params); txtLoad.setId(txtloadid); txtLoad1.setHint("Ety Cage"); int txtload1id = j+50; txtLoad1.setId(txtload1id); txtLoad2.setHint("Load"); int txtload2id = j+100; txtLoad2.setId(txtload2id); txtLoad3.setHint("Ld Cage"); int txtload3id = j+150; txtLoad3.setTextSize(18); txtLoad3.setId(txtload3id); ll.addView(txt); ll.addView(txtLoad); ll.addView(txtLoad1); ll.addView(txtLoad2); ll.addView(txtLoad3); //Add fields to LinearLayout defined in XML mainLinear.addView(ll); count++; j=count; } }); )读取我的应用文件中的txt文件。

所以我尝试在另一个文件中执行操作,但是我无法导入数据库(数据库)

它给我发了一个错误:

open('filename')

目录:

ImportError: cannot import name 'db' from '__main__' (/home/chrys/deploy/initdata.py)

此处为initdata代码:

deploy/
      |api.py      #the app file 
      |initdata.py # the file where i try to import title fom txt file

这里是api代码:

from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy


from . import db
from api import Movie

file = open("movie_t-utf8.txt", "r")
to_add =[]
for line in file.readlines():
    item = line.split('|')
    if item[1][-1:] == '\n':
        item[1] = item[1][:-1]
    film = Movie(id = item[0], title=item[1])
    db.session.add(film)
db.session.commit()

在此先感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我不确定您到底想做什么,但是如果您只想从txt导入数据库,则应该考虑使用Flask-Script。

我的一个项目的示例:

from config import Config
from edulya.app import create_app

app = create_app(Config)

from flask_script import Manager
from edulya.app import db

path = os.path.realpath(os.path.dirname(__file__))

manager = Manager(app)

@manager.command
def init_schools():
    with open(
        os.path.join(
            path, 'edulya', 'static_data', 'fr-en-annuaire-education.json'
        ), 'r'
    ) as file:
        json_data = ujson.loads(file.read())
        for data in json_data:
            School.from_json(data)

和School.from_json看起来像这样:

class School(db.Model):
    @staticmethod
    def from_json(data):
        school = School(
           id_commune=data['code_commune'],
            id_academie=data['code_academie'],
            status=data['statut_public_prive'],
            updated=datetime.strptime(data['date_maj_ligne'], '%Y-%m-%d')
        )

        db.session.add(school)
        db.session.commit()

然后是py manager.py init_schools