3

I have created a structure of a basic Flask application and I need guidance with executing a sql script. My folder structure is this:

-simpleblog (name of app)

  flask (contains the virtual environment, 3 folders)
  app
    static (css, js)
    templates (html)
    __init__.py
  config.py
  run.py

I have a sql script with the definition of one table, and I don't know where to place it and how to execute it. I know sqlite3 is part of python, but must I install anything else?

1
  • what is the SQL script ? can you please share ? Commented May 10, 2013 at 13:11

1 Answer 1

1

Look at this tutorial from flask website that explains how to use a sqllite database and execute sql scripts along with a suggested folder structure. Usually, you can create a schema.sql file and put it in the main app folder. In your example, you can put it as:

app
    static (css, js)
    templates (html)
    __init__.py
    config.py
    run.py
    schema.sql # your sql script

http://flask.pocoo.org/docs/tutorial/schema/#tutorial-schema

To create the initial database, use this. But you do need to install sqllite3 first.

http://flask.pocoo.org/docs/tutorial/dbinit/#tutorial-dbinit

Basically, it lets you create your initial database by running the following command:

sqlite3 /tmp/flaskr.db < schema.sql

Lets understand the above command. we have the database creation scripts (CREATE,DROP etc.) in schema.sql. We take that as a input to the sqlite3 executable and create the database at /tmp/flaskr.db. Note: you might need to run the following command first to generate a blank flaskr.db file

touch /tmp/flaskr.db
Sign up to request clarification or add additional context in comments.

3 Comments

This is the tutorial that I follow, I was reading its pdf version and I missed the bit about where to place the file. Now, regarding executing the script I cannot understand the following "Such a schema can be created by piping the schema.sql file into the sqlite3 command as follows:" how do I install sqlite3 in my virtual environment? I tried with pip and failed.
Ok I made to work, thank you very much; let me ask you, what is the point of putting this db file in a tmp folder? I thought this is a database driven app, so the db file is of primary importance? Also, is there a GUI tool to query this database, just for convenience.
good question. I think the tutorial just uses the tmp folder to demonstrate but realistically, you will not. the tutorial lets you delete and re-create the db and hnce using a tmp folder i guess.

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.