FORMAT SQL QUERY OUTPUT INTO AN HTML TABLE
From my own experience with python SQL connector. I think this is one of the easiest way to format the output SQL query output into HTML table code.
Steps to convert SQL query output into HTML table code:
- Connect to the database using SQL connector
- Create a cursor to execute your SQL query and fetch the resultant
- Execute the SQL query and use the below code to convert the output into an HTML table.
Convert SQL query output into HTML table | Python |
For example, I want to convert the above SQL query output into HTML code and I am using Snowflake Connector.
snowflake = snowflake.connector.connect(
user=SF_USER,
password=SF_PASSWORD,
account=SF_ACCOUNT,
warehouse=SF_WAREHOUSE,
database=SF_DATABASE,
schema=SF_SCHEMA
)
snowflake_cur = snowflake.cursor()
sHtml=""
sql = """
SELECT CustomerID, CustomerName FROM Customers ORDER BY CustomerName ASC;
"""
snowflake_cur.execute(sql)
sHtml=sHtml+"""</br><table order="1"><tr><th>CustomerID</th><th>CustomerName</th></tr>"""
for row in snowflake_cur.fetchall():
sHtml=sHtml+"<tr><td>"+str(row[0])+"</td><td style=""text-align:center"" >"+str( "{:,}".format(row[1]))+"</td></tr>"
sHtml=sHtml+"</table></br>"
No comments:
Post a Comment