{"library":"mysql-python","title":"mysql-python (Legacy MySQLdb)","description":"mysql-python, also widely known as MySQLdb, is a Python 2.x interface to the popular MySQL database server. The last official release, 1.2.5, dates back to 2014 and primarily supports Python versions 2.4 through 2.7. It adheres to the Python Database API Specification v2.0 (PEP 249). For modern Python 3.x development, this library is considered deprecated, with `mysqlclient` (a actively maintained fork providing a compatible API) and `mysql-connector-python` (Oracle's official pure Python driver) being the recommended alternatives.","language":"python","status":"deprecated","last_verified":"Thu Apr 16","install":{"commands":["pip install mysql-python"],"cli":null},"imports":["import MySQLdb"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import MySQLdb\nimport os\n\ntry:\n    # Establish a connection to the MySQL server\n    # Note: mysql-python (MySQLdb) primarily supports Python 2.x. \n    # Use 'passwd' for password and 'db' for database name.\n    conn = MySQLdb.connect(\n        host=os.environ.get('MYSQL_HOST', 'localhost'),\n        user=os.environ.get('MYSQL_USER', 'root'),\n        passwd=os.environ.get('MYSQL_PASSWORD', 'password'),\n        db=os.environ.get('MYSQL_DATABASE', 'testdb')\n    )\n\n    # Create a cursor object to execute queries\n    cursor = conn.cursor()\n\n    # Create a table (if it doesn't exist)\n    cursor.execute(\"\"\"\n        CREATE TABLE IF NOT EXISTS my_table (\n            id INT AUTO_INCREMENT PRIMARY KEY,\n            name VARCHAR(255),\n            age INT\n        )\n    \"\"\")\n    conn.commit() # Commit changes for DDL operations\n\n    # Insert data\n    cursor.execute(\"INSERT INTO my_table (name, age) VALUES (%s, %s)\", (\"Alice\", 30))\n    cursor.execute(\"INSERT INTO my_table (name, age) VALUES (%s, %s)\", (\"Bob\", 24))\n    conn.commit() # Commit changes for DML operations\n\n    print(f\"Inserted {cursor.rowcount} records.\")\n\n    # Fetch data\n    cursor.execute(\"SELECT id, name, age FROM my_table\")\n    rows = cursor.fetchall()\n\n    print(\"\\nData in my_table:\")\n    for row in rows:\n        print(f\"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}\")\n\nexcept MySQLdb.Error as e:\n    print(f\"Error: {e}\")\nfinally:\n    # Close the cursor and connection\n    if 'cursor' in locals() and cursor:\n        cursor.close()\n    if 'conn' in locals() and conn:\n        conn.close()\n    print(\"\\nConnection closed.\")","lang":"python","description":"This quickstart demonstrates how to connect to a MySQL database, create a table, insert data, and fetch results using the `MySQLdb` module. Ensure your MySQL server is running and `MYSQL_HOST`, `MYSQL_USER`, `MYSQL_PASSWORD`, and `MYSQL_DATABASE` environment variables are set with appropriate credentials. Remember to commit changes after DDL and DML operations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}