0

I'm following the Flask tutorial to try to make my own thing. I'm getting an error in step 5 when I try to initialize the database.

When I type flask initdb into terminal, this is the error I get: seei5.py line 24, in init_db db.cursor().executescript(f.read()) sqlite3.OperationalError: near "drop": syntax error

I'm using python 3.6

This is my file structure:

/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()

1 Answer 1

1

Can you fix your 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));

You have missing ;

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.