Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- # Replace with your actual database credentials
- user = ''
- password = ''
- host = ''
- database = ''
- try:
- # Establish a connection
- conn = mysql.connector.connect(user=user, password=password, host=host, database=database)
- # Create a cursor
- cursor = conn.cursor()
- # Execute a simple query
- cursor.execute("SELECT 'Connection successful!'")
- # Fetch and print the result
- result = cursor.fetchone()
- print(result[0])
- except mysql.connector.Error as err:
- print(f"Error: {err}")
- finally:
- # Close the cursor and connection
- if 'cursor' in locals():
- cursor.close()
- if 'conn' in locals():
- conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement