Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find where sqlite3 database is

I'm folowing Flaskr Tutorial (http://flask.pocoo.org/docs/tutorial/setup/) and missunderstand one thing:

Both aplication that I did and the original Flaskr from github (https://github.com/mitsuhiko/flask/tree/master/examples/flaskr/) work properly, but I dont see any database files created, even though I manually created '/tm/flaskr.db', this file is still empty.

Please, could you explain me, in what a magic place sqlite3 keeps data?

here is py file that I execute:

# coding: utf-8
import sqlite3
import inspect
from flask import Flask, request, session, g, redirect, url_for, abort, render_template,                 flash
from contextlib import closing

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

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

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read())
        db.commit()
@app.before_request
def before_request():
    g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
    g.db.close()

@app.route('/')
def show_entries():
    cur = g.db.execute('select title, text from entries order by id desc')
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
    return render_template('show_entries.html', entries=entries)

@app.route('/add', methods=['POST'])
def add_entry():
    if not session.get('logged_in'):
        abort(401)
    g.db.execute('insert into entries (title, text) values (?, ?)',
                 [request.form['title'], request.form['text']])
    g.db.commit()
    flash('New entry was successfully posted')
    return redirect(url_for('show_entries'))

@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != app.config['USERNAME']:
            error = 'Invalid username'
        elif request.form['password'] != app.config['PASSWORD']:
            error = 'Invalid password'
        else:
            session['logged_in'] = True
            flash('You were logged in')
            return redirect(url_for('show_entries'))
    return render_template('login.html', error=error)

@app.route('/logout')
def logout():
    session.pop('logged_in', None)
    flash('You were logged out')
    return redirect(url_for('show_entries'))

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

here is config file:

# coding: utf-8
DATABASE = '/tmp/site.db'
DEBUG = True
SECRET_KEY = 'dev_key'
USERNAME = 'admin'
PASSWORD = 'pass'

schema.sql

drop table if exists entries;
create table entries (
  id integer primary key autoincrement,
  title string not null,
  text string not null
);

Thanks so much for your answers, I've noticed, that db is created in the '/tmp/'folder in the root of file system. I expected, that it would be created in my project folder like in django with sqlite.

like image 463
user2678368 Avatar asked Jun 17 '26 06:06

user2678368


1 Answers

The database will be stored in "/tmp/site.db".

try doing sqlite3 /tmp/site.db

If you want to keep this file in the projects directory just change it to site.db

You need the init_db() to be run seperately just once.

From Python command line import flaskr and run flaskr.init_db()

I would suggest looking at Flaskr and trying to understand it first. The default Flaskr will re-initiated the db every time it is executed.

Joe

like image 93
Joe Doherty Avatar answered Jun 19 '26 19:06

Joe Doherty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!