import mysql.connector as mc

try:
    # Establish connection to the MySQL database
    conn = mc.connect(
        user="jenyarayvat",
        host="44.226.117.35",
        password="jenyarayvat@320$789456",
        database="instruments"
    )

    if conn.is_connected():
        print("Connected to the MySQL database")

    cursor = conn.cursor()

    # Execute SQL query to fetch data
    query = """
        SELECT `Symbol.1`
        FROM zerodha_nse
        WHERE `exchange` = 'NSE'
        AND `segment` = 'NSE'
        AND `instrument_type` = 'EQ';
    """
    cursor.execute(query)

    # Fetch and display results
    checkSymbols = cursor.fetchall()
    for data in checkSymbols:
        print(data)

except mc.Error as e:
    print(f"Error connecting to the database: {e}")
finally:
    # Ensure proper resource cleanup
    if 'cursor' in locals() and cursor is not None:
        cursor.close()
    if 'conn' in locals() and conn.is_connected():
        conn.close()
        print("Database connection closed")

