Forums

Flask sqlite3 help

Hello, I'm trying to get a Flask local webapp online and am having problems accessing a database using in sqlite3. On my local machine I have used the absolute address:

conn = sqlite3.connect("C:\\Users\\Lenovo\\PycharmProjects\\spacedonline\\down.db"): : :

On PA I have altered the code to:

conn = sqlite3.connect("down.db")

But it spits out the error code:

sqlite3.OperationalError: no such table: Students

I have no idea what I'm doing, so anything that puts me on the right path would be much appreciated.

Edit: I've also tried using the absolute path and the Os join method.

The error you see suggests that there are no tables found -- you need to create them. Look at the command db.create_all() in this blog post.

Thanks for the reply. The tables are definitely there, I have even checked by downloading it from PA and opening it to make sure that the tables are still there.

Could you show the full traceback error message your getting?

Sure:

2021-12-23 17:32:52,832: Error running WSGI application
2021-12-23 17:32:52,833: sqlite3.OperationalError: no such table: Students
2021-12-23 17:32:52,833:   File "/var/www/downlandsmaths_pythonanywhere_com_wsgi.py", line 16, in <module>
2021-12-23 17:32:52,833:     from spacedonline import app as application  # noqa
2021-12-23 17:32:52,833: 
2021-12-23 17:32:52,834:   File "/home/DownlandsMaths/mysite/spacedonline.py", line 5, in <module>
2021-12-23 17:32:52,834:     from forms import selectclassform, LoginForm, RegistrationForm, ValidateTopicForm, selectClass2, add1studentForm, dynamicform, changeClass
2021-12-23 17:32:52,834: 
2021-12-23 17:32:52,834:   File "/home/DownlandsMaths/mysite/forms.py", line 38, in <module>
2021-12-23 17:32:52,834: 
2021-12-23 17:32:52,834:   File "/home/DownlandsMaths/mysite/forms.py", line 42, in add1studentForm
2021-12-23 17:32:52,834:     conn = sqlite3.connect('/home/downlandsmaths/mysite/down.db')
2021-12-23 17:32:52,835: ***************************************************
2021-12-23 17:32:52,835: If you're seeing an import error and don't know why,
2021-12-23 17:32:52,835: we have a dedicated help page to help you debug: 
2021-12-23 17:32:52,835: https://help.pythonanywhere.com/pages/DebuggingImportError/
2021-12-23 17:32:52,835: ***************************************************

Make sure that you were checking this database: /home/downlandsmaths/mysite/down.db and not a different one. If you were changing paths and/or moving your code around the files, it's possible that Students table was created in a different db. You can also check where Students table (class) is defined in your app code and make sure that it was executed when you were creating the tables?

I've checked again and it's definitely there. The Students table wasn't defined in my app code, it's a pre-existing db. Like I say, it works fine on my machine. Here's the code for where it is referenced in the main file:

@app.route("/addsinglestudent", methods=["GET", "POST"])
def addsinglestudent():
form= add1studentForm()
if form.validate_on_submit():
    add1student(form.studentName.data, form.nameofclass.data)
    flash("Student added")
    return redirect(url_for("hello_world"))

Okay, think I've found the combination that works

Can you share what worked for you