How to create tables and implement them into Flask:

Student EntryWeb Development
  1. Create the actual tables in your database using MySQL
  2. Use mysql.connector (example #1) to connect the database with your Flask app
  3. Use queries to run through your database and retrieve/insert information (examples #2 and #3)

Example 1:

import mysql.connector
connection = mysql.connector.connect(
host='---.---.---.--',
user='uahqojmy_TaSharma',
password='*********',
database='uahqojmy_student_TaSharma'
)

Example 2:

lab_query = '''
SELECT l.display_name
FROM Lab l
JOIN User_Lab ul ON l.Id = ul.lab_id
WHERE ul.user_id = %s
'''
cursor.execute(lab_query, (user_id,))
lab_display_name = cursor.fetchall()

Example 3:

insert_query = '''

    INSERT INTO Request (Lab_Id, Requested_Date, Description, Unit_Price, Quantity,

                        OAC_Recommended_Date, Request_Type_Id, Additional_Notes, User_Id, State_Id)

    VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)

    '''

    # Execute the query with the provided form data and User_Id

    cursor.execute(insert_query, (

        matching_lab_id, requested_date, description, unit_price, quantity,

        oac_recommended_date, request_type_id, additional_notes, user_id, 3

    ))

    # Commit the changes to the database

    connection.commit()

Leave a Reply

Your email address will not be published. Required fields are marked *