Home > Blockchain >  how can I display a SQL table in flask?
how can I display a SQL table in flask?

Time:02-06

Is there a way I can display a SQL table in HTML and CSS using flask? If so please leave the code down below, or make a GitHub directory so I can have a look!

CodePudding user response:

Here is what I used in my older projects while using Microsoft SQL.

###### IMPORTED LIBRARIES ######  
from flask import render_template, Flask
import pyodbc

###### SQL CONNECTION ######
connection = pyodbc.connect('Driver={SQL Server};'
                      'Server=DESKTOP-NP0I4J5\SQLEXPRESS;'
                      'Database=REPLICATED_STORAGE;'
                      'Trusted_Connection=yes;')

cursor = connection.cursor()    
cursor.execute("SELECT * FROM FRUITS")
data = cursor.fetchall()

###### FLASK SERVER ######
headings = ("COLOR", "TYPE", "NAME", "WEIGHT")
id = 80809915

app = Flask(__name__)
@app.route('/')    
def index():    
    return render_template("table.html", headings=headings, data=data)

if (__name__ == '__main__'):
    app.run()
  •  Tags:  
  • Related