Search code examples
pythonhtmlsqliteflaskbootstrap-table

Display Query from SQLite3 Database into Bootstrap table


i want to display my query from SQLite3 into Bootstrap Table. but it seems doesnt work and wont display the table. herewith my code

views.py

@main.route('/currentlist', methods=["GET"])
def current():
    if request.method == 'GET':
        Connection = sqlite3.connect('C:\\Backup_old\\Task\\e_ticket\\my_app\\db\\customer.db')
        cursor = Connection.cursor()
        query2 = "SELECT * from customer"
        cursor.execute(query2)
        test = cursor.fetchall()
        return render_template('overview.html', testform = test)

HTML

<div class= "content">

      <table id="dtBasicExample" class="table" width="100%">
        <thead>
          <tr>
            <th class="th-sm">Date
            </th>
            <th class="th-sm">time
            <th class="th-sm">customer
            </th>
            <th class="th-sm">pic
            </th>
            <th class="th-sm">category
            </th>
            <th class="th-sm">description
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>9 December 2021</td>
            <td>15:30:00</td>
            <td>AB Corp.</td>
            <td>Bob</td>
            <td>network</td>
            <td>network is down</td>
          </tr>
          <tr>
            <td>9 December 2021</td>
            <td>17:30:00</td>
            <td>AB Corp.</td>
            <td>Alex</td>
            <td>computer</td>
            <td>computer is broken</td>
          </tr>
          <tr>
            <td>10 December 2021</td>
            <td>05:32:00</td>
            <td>CD Corp.</td>
            <td>Bob</td>
            <td>Server</td>
            <td>server is down</td>
          </tr>
          <tr>
            <td>12 December 2021</td>
            <td>10:30:00</td>
            <td>AB Corp.</td>
            <td>Bob</td>
            <td>printer</td>
            <td>printer is down</td>
          </tr>


        </tbody>
        
      </table>
    



    </div>

it should be like this but with the data from DB enter image description here

i've read many similiar question like this, but most of them use SQLAlchemy.

Any help greatly appreciated!


Solution

  • You need to use a loop and template variables with the data.

    <tbody>
      {% for row in testform %}
      <tr>
        <td>{{ row[0] }}</td>
        <td>{{ row[1] }}</td>
        <td>{{ row[2] }}</td>
        <td>{{ row[3] }}</td>
        <td>{{ row[4] }}</td>
        <td>{{ row[5] }}</td>
      </tr>
      {% endfor %}
    </tbody>