PythonAnywhere Forums

flask sqlite not connecting?

This works in bash:

import sqlite3
db = sqlite3.connect('/home/joegillon/mysite/adlai.db')

I can run a query. But the same thing returns a NoneType db in my app.

Are you sure you're using the full path to the file in your app and not a relative path?

Ah, Glenn the guru. Exact same path sting in app as in console.

I'm not clear on what you're trying to say. Are you saying that was the problem, or that it wasn't the problem?

Not. I have the same string in each case.

So this is happening when I try to get a cursor. If I remember correctly, the times I've supposedly connected to an sqlite DB but didn't, the error didn't come when getting a cursor, but later when trying to execute a query. So maybe the problem isn't the path. Maybe it's finding the DB file, but for some reason not returning a db object?

I had a working app not using an ORM but needed login so I glommed flask-security onto it, using sqlalchemy. So now the app is connecting two ways to the same sqlite file, with sqlalchemy and without. Think that's the problem? Is there some reason an app can only have 1 connection to sqlite?

Multiple uses of the same sqlite database should work, however, the entire database is locked for writes when one of them is writing to it.

Did you ever solve this? I'm running into the same issue - app and db are all at the same path, the db is there and I can connect to it and select data from bash, the error comes not on the cursor but on actually trying to execute the query. Any ideas?

Actually, nm - turns out I just needed to use the full path to my db (e.g. '/home/username/mysite/my.db').