2

I'm using this tutorial for learning flask. In the second paragraph it says to use this command:

sqlite3 /tmp/flaskr.db < schema.sql

But I'm using Windows 8. What can I do instead of that command? This is my sql code:

drop table if exists entries;
create table entries (
    id integer primary key autoincrement,
    title text not null,
    text text not null
);
2
  • I think this is sqlite3.exe. So you can call it with parameters from your Cmd. But maybe a bit different Commented Jan 11, 2016 at 18:12
  • no i cant use this command in sqlite3. i tested it. can you tell it how can i call it from cmd? Commented Jan 11, 2016 at 19:05

1 Answer 1

2

Just keep following the tutorial by adding the init_db method and running the following python script:

# all the imports
import sqlite3
from flask import Flask
from contextlib import closing

# configuration
DATABASE = './flaskr.db'
DEBUG = True


# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)

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


def connect_db():
    return sqlite3.connect(app.config['DATABASE'])

if __name__ == '__main__':
    init_db()
    #app.run()

to make it simple, database file flaskr.db will be created in the current directory and schema.sql is supposed to be there too ...

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

2 Comments

thank you very much.this script worked for me. but let me ask 2 questions:can i add SECRET_KEY and USERNAME and PASSWORD to this script?what is the difference between this script and using .read schema.sql and save it as flaskr.db on sqlite3.exe?
yes you can add the other fields such as SECRET_KEY, USERNAME in the script and it has the same effect as running sqlite3 flaskr.db < schema.sql so you don't really need the commandline ...

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.