I am developing a Flask application and I have a webpage where I want to display an existing SQLite Database table I have.
Here is my python file:
import random
from flask import Flask, redirect, render_template, request, session
import sys; sys.path
import sqlite3
import pandas as pd
app = Flask(__name__)
sql_connect = sqlite3.connect('questions.sqlite3', check_same_thread = False)
cursor = sql_connect.cursor()
@app.route("/database")
def database():
MCQTable = cursor.execute("SELECT * FROM MCQ;").fetchall()
return render_template("database.html", MCQTable = MCQTable)
Here is my html code in /database route, where my columns in my table are "question", "ChoiceA", "ChoiceB", "ChoiceC", "ChoiceD", "CorrectAnswer":
<table style="width: 100%">
<tr>
<th>Question</th>
<th>ChoiceA</th>
<th>ChoiceB</th>
<th>ChoiceC</th>
<th>ChoiceD</th>
<th>Correct Answer</th>
</tr>
{% for row in MCQTable %}
<tr>
<th>{{ row["question"] }}</th>
<th>{{ row["ChoiceA"] }}</th>
<th>{{ row["ChoiceB"] }}</th>
<th>{{ row["ChoiceC"] }}</th>
<th>{{ row["ChoiceD"] }}</th>
<th>{{ row["CorrectAnswer"] }}</th>
</tr>
{% endfor %}
</table>
However, when I did a test, the row["column"] doesn't work. I know that there are no problems in my SQL table because I did a test before where I selected a column instead of the whole table in my python and it worked fine. However, I want to do it this way as I think it is more efficient.