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.



Thursday, January 11, 2024

Python 3 : The Class

 class kelas1:  
   def __init__(self, nama, usia):  
     self.nama = nama  
     self.usia = usia  
   def __str__(self):  
     return "Tampilkan nama : %s , usia : %s" % (self.nama, self.usia)  
 isian = kelas1("Steven", 17)  
 print(isian)  

This is the Initial Source Code for Learning About Classes in Python Programming.

Understanding classes in Python programming will be useful for understanding how to create tables using Flask Table.

A class is a group of functions.

We want to learn how to inherit in classes in Python programming. We can only inherit within the same class, which is inheritance between functions.