Advertisement
DrAungWinHtut

python_db_server.py

Jan 13th, 2024
1,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. import mysql.connector
  2.  
  3. # Replace with your actual database credentials
  4. user = ''
  5. password = ''
  6. host = ''
  7. database = ''
  8.  
  9. try:
  10.     # Establish a connection
  11.     conn = mysql.connector.connect(user=user, password=password, host=host, database=database)
  12.  
  13.     # Create a cursor
  14.     cursor = conn.cursor()
  15.  
  16.     # Execute a simple query
  17.     cursor.execute("SELECT 'Connection successful!'")
  18.  
  19.     # Fetch and print the result
  20.     result = cursor.fetchone()
  21.     print(result[0])
  22.  
  23. except mysql.connector.Error as err:
  24.     print(f"Error: {err}")
  25.  
  26. finally:
  27.     # Close the cursor and connection
  28.     if 'cursor' in locals():
  29.         cursor.close()
  30.     if 'conn' in locals():
  31.         conn.close()
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement