Tuesday, January 30, 2024

Flask: Printing Database Query Results to an HTML Table

In this example, I am trying to query a MariaDB database. Then I am trying to display the results on a web page. However, for the first step, I will try to display the query results to the terminal. The following is the source code for printing database query results to the terminal:


 import mariadb  
 import sys  
 koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.6",port=3306,database="saham")  
 kursor = koneksi.cursor(dictionary=True)  
 kursor.execute("SELECT idnama1,namaawal1,namaakhir1 FROM nama1")  
 hasil = kursor.fetchall()  
 print(hasil[0])  

The following source code successfully prints the query results from the MariaDB database to the terminal.


 import mariadb  
 import sys  
 koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.7",port=3306,database="saham")  
 kursor = koneksi.cursor(dictionary=True)  
 kursor.execute("SELECT idnama1,namaawal1,namaakhir1 FROM nama1")  
 hasil = kursor.fetchall()  
 print(hasil[1])  


 import mariadb  
 import sys  
 koneksi = mariadb.connect(user="steven",password="kucing",host="1.1.3.7",port=3306,database="saham")  
 kursor = koneksi.cursor(dictionary=True)  
 kursor.execute("SELECT idnama1,namaawal1,namaakhir1 FROM nama1")  
 hasil = kursor.fetchall()  
 idnama1A = hasil[0]["idnama1"]  
 print(idnama1A)  

The following is the source code for rendering an HTML page using Flask.



No comments:

Post a Comment