2017-09-02 1 views
-1

Je suis le flacon tutorial pour essayer de faire mon propre truc. J'obtiens une erreur dans step 5 lorsque j'essaie d'initialiser la base de données.Flask erreur initalizing base de données

Lorsque je tape flask initdb dans le terminal, c'est l'erreur que je reçois: seei5.py ligne 24, dans init_db db.cursor() ExecuteScript (f.read()) sqlite3.OperationalError:. Près de " drop ": erreur de syntaxe

J'utilise python 3.6

Ceci est ma structure de fichier:

/seei5 
    /seei5 
     __init__.py 
     /static 
     /templates 
     seei5.py 
     schema.sql 
    setup.py 
    MANIFEST.in 

MANIFEST.in

graft seei5/templates 
graft seei5/static 
include seei5/schema.sql 

schema.sql

drop table if exists books; 
create table books (
    book_id integer, 
    title text, 
    PRIMARY KEY (book_id)) 


drop table if exists chapters; 
create table chapters (
    chapter_id integer, 
    chapter text, 
    book_id integer, 
    PRIMARY KEY (chapter_id), 
    FOREIGN KEY (book_id) REFERENCES books (book_id)) 


drop table if exists concepts; 
create table concepts (
    concepts_id integer, 
    concept text, 
    definition text, 
    chapter_id integer, 
    PRIMARY KEY (concepts_id), 
    FOREIGN KEY (chapter_id) REFERENCES chapters (chapter_id)) 

seei5.py

import os 
import sqlite3 
from flask import * 

app = Flask(__name__) 
app.config.from_object(__name__) 

app.config.update(dict(
    DATABASE=os.path.join(app.root_path, 'seei5.db'), 
    SECRET_KEY='development key', 
    USERNAME='admin', 
    PASSWORD='default' 
)) 
app.config.from_envvar('SEEI5_SETTINGS', silent=True) 

def connect_db(): 
    rv = sqlite3.connect(app.config['DATABASE']) 
    rv.row_factory = sqlite3.Row 
    return rv 

def init_db(): 
    db = get_db() 
    with app.open_resource('schema.sql', mode='r') as f: 
     db.cursor().executescript(f.read()) 
    db.commit() 

@app.cli.command('initdb') 
def initdb_command(): 
    init_db() 
    print("Initialized the database.") 

def get_db(): 
    if not hasattr(g, 'sqlite_db'): 
     g.sqlite_db = connect_db() 
    return g.sqlite_db 

@app.teardown_appcontext 
def close_db(error): 
    if hasattr(g, 'sqlite_db'): 
     g.sqlite_db.close() 

Répondre

1

Pouvez-vous fixer votre schema.sql

schema.sql

drop table if exists books; 
create table books (
    book_id integer, 
    title text, 
    PRIMARY KEY (book_id)); 


drop table if exists chapters; 
create table chapters (
    chapter_id integer, 
    chapter text, 
    book_id integer, 
    PRIMARY KEY (chapter_id), 
    FOREIGN KEY (book_id) REFERENCES books (book_id)); 


drop table if exists concepts; 
create table concepts (
    concepts_id integer, 
    concept text, 
    definition text, 
    chapter_id integer, 
    PRIMARY KEY (concepts_id), 
    FOREIGN KEY (chapter_id) REFERENCES chapters (chapter_id)); 
Il vous manque ;