Forums

SQLite3 Connection Issues

Howdy everyone,

I'm trying to get some info on how to fix a little issue I'm having. I have a database I'm trying to pull information from with sqlite3. Everything I do works great locally but for some reason it does't translate when I post it here. I have a beginner account but I can't find anywhere that that would be an issue (if it is then great! at least I'll know the problem).

The url is tlamanna42.pythonanywhere.com/sqltest

And the main app:

# A very simple Flask Hello World app for you to get started with...f
from functions1 import Test
import urllib
from flask import Flask, render_template, g
import sqlite3

app = Flask(__name__)

app.secret_key = "my precious"
app.database = "posts.db"
x = Test()
y = urllib.urlopen('http://www.google.com')
z = y.read()

@app.route('/')
def hello_world():
    return 'Hello from Flask!'

@app.route('/test')
def test():
    return 'Test'

@app.route('/111')
def xxx():
   return x.test()

@app.errorhandler(404)
def error(e):
    return 'No'

@app.route('/urllib')
def xxxx():
    return z

@app.route('/sqltest')
def sqltest():
    g.db = connect_db()
    cur = g.db.execute('select * from posts')
    posts1 = [dict(title=row[0],description=row[1]) for row in cur.fetchall()]
    g.db.close()
    return render_template('sqltest.html', posts1=posts1)

@app.route('/welcome')
def welcome():
    post = 'post'
    return render_template('welcome.html', post=post)

def connect_db():
    return sqlite3.connect(app.database)

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

Thank you so much.

My guess is that it's about paths. I would change the app.database line so that it has a full path, eg

app.database = "/home/TLamanna42/mysite/posts.db"

Your guess was 100% correct. Thanks a million!

:)

not working for me

Then you're using the wrong path to the database. Make sure that the path you specify in the connection string matches where the file actually is.