{"id":4624,"library":"mariadb","title":"MariaDB Connector/Python","description":"MariaDB Connector/Python is a Python extension that enables Python applications to connect to MariaDB and MySQL databases. It provides a standard DB-API 2.0 interface and offers features like client-side and server-side cursors, prepared statements, and support for various MariaDB-specific features. The current stable version is 1.1.14, with a 2.0.0 release candidate bringing significant new features like async support. Releases occur a few times a year for minor versions, with major versions less frequent.","status":"active","version":"1.1.14","language":"en","source_language":"en","source_url":"https://www.github.com/mariadb-corporation/mariadb-connector-python","tags":["database","mariadb","mysql","sql","connector","db-api"],"install":[{"cmd":"pip install mariadb","lang":"bash","label":"Install stable version"},{"cmd":"pip install --pre mariadb","lang":"bash","label":"Install pre-release (e.g., 2.0.0rc)"}],"dependencies":[],"imports":[{"symbol":"mariadb","correct":"import mariadb"},{"note":"While `from mariadb import Error` works, `mariadb.Error` is often used for clarity and to avoid name collisions.","wrong":"from mariadb import Error","symbol":"Error","correct":"import mariadb.Error"}],"quickstart":{"code":"import mariadb\nimport os\n\nhost = os.environ.get('MARIADB_HOST', '127.0.0.1')\nport = int(os.environ.get('MARIADB_PORT', 3306))\nuser = os.environ.get('MARIADB_USER', 'root')\npassword = os.environ.get('MARIADB_PASSWORD', '')\ndatabase = os.environ.get('MARIADB_DATABASE', 'test_db')\n\nconn = None\ncursor = None\ntry:\n    # Connect to MariaDB Platform\n    conn = mariadb.connect(\n        user=user,\n        password=password,\n        host=host,\n        port=port,\n        database=database\n    )\n\n    # Get a cursor\n    cursor = conn.cursor()\n\n    # Create a table\n    cursor.execute(\"CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255))\")\n    conn.commit()\n\n    # Insert data\n    cursor.execute(\"INSERT INTO users (name) VALUES (?) \", (\"Alice\",))\n    cursor.execute(\"INSERT INTO users (name) VALUES (?) \", (\"Bob\",))\n    conn.commit()\n\n    # Query data\n    cursor.execute(\"SELECT id, name FROM users\")\n    for (id_val, name) in cursor:\n        print(f\"ID: {id_val}, Name: {name}\")\n\nexcept mariadb.Error as e:\n    print(f\"Error connecting to or interacting with MariaDB Platform: {e}\")\nfinally:\n    if cursor:\n        cursor.close()\n    if conn:\n        conn.close()","lang":"python","description":"This quickstart demonstrates how to connect to a MariaDB database, create a table, insert data using parameterized queries, and fetch results. It retrieves connection details from environment variables for secure and flexible deployment. Remember to set `MARIADB_HOST`, `MARIADB_PORT`, `MARIADB_USER`, `MARIADB_PASSWORD`, and `MARIADB_DATABASE`."},"warnings":[{"fix":"Consult the official 2.x release notes and documentation for specific migration paths. If developing new applications, consider starting with 2.x for async capabilities, otherwise stick to 1.x for current stability.","message":"MariaDB Connector/Python 2.x (currently in release candidate) introduces significant breaking changes from 1.x, particularly concerning asynchronous support, connection string URIs, and potentially API signatures. Code written for 1.x will likely require modifications to work with 2.x.","severity":"breaking","affected_versions":"2.0.0rc and later"},{"fix":"Ensure the necessary system-level development libraries for MariaDB Connector/C are installed before attempting to install the Python package. Common errors include 'mariadb.Error: Can't find libmariadb.so' or compilation failures.","message":"The `mariadb` connector is a native C extension. If pre-compiled wheels are not available for your platform/Python version, or if you're installing from source, you may need to install the MariaDB Connector/C library (e.g., `libmariadb-dev` on Debian/Ubuntu, `mariadb-connector-c-devel` on Fedora/RHEL) on your system before `pip install mariadb`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Rewrite any queries that concatenate user input directly into SQL strings to use the `execute()` method's parameter substitution feature, where `?` acts as a placeholder for positional arguments.","message":"Always use parameterized queries (e.g., `cursor.execute(\"INSERT INTO table VALUES (?) \", (value,))`) instead of string formatting or f-strings for passing user-supplied data into SQL queries. Failing to do so makes your application vulnerable to SQL injection attacks.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Explicitly call `conn.close()` and `cursor.close()` in a `finally` block, or preferably, use `with` statements for connections and cursors if supported by the API (though not directly for `mariadb.connect` itself, cursors can often be managed this way if the connection object supports context management for cursors).","message":"Connections and cursors should always be properly closed to release database resources. Failing to do so can lead to resource exhaustion and performance issues, especially in high-traffic applications.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}