import mysql.connector

def connect_and_query():
    try:
        # Connect to your database
        conn = mysql.connector.connect(
            host='localhost',
            user='OG07ZTQ8VS_ulocalu',
            password='14c1_Kc6d',
            database='admin_dev_JL3J2UYOQA_ulocalu_E4UHKC'
        )

        cursor = conn.cursor()

        # SQL query to select records
        query = "SELECT * FROM site_submission WHERE scraped != 1"

        cursor.execute(query)

        # Fetch all records where 'scraped' is not equal to 1
        records = cursor.fetchall()

        # Open a file to write
        with open('output.txt', 'w') as f:
            f.write("Fetched records:\n")
            for row in records:
                # Convert row to string and write to file
                f.write(str(row) + "\n")

        print("Records have been written to output.txt")

    except mysql.connector.Error as e:
        print(f"Error connecting to MySQL Platform: {e}")
    finally:
        if conn.is_connected():
            cursor.close()
            conn.close()
            print("MySQL connection is closed")

if __name__ == "__main__":
    connect_and_query()

