MySQL Connector/Python (RF variant)
An old, unmaintained variant of the pure Python MySQL driver for Python, implementing the DB API v2.0 specification (PEP-249). This package was released to PyPI to address distribution issues with the official 'mysql-connector-python' around 2015-2017. It is currently at version 2.2.2 and has not been updated since February 2017.
Common errors
-
ModuleNotFoundError: No module named 'mysql.connector'
cause The `mysql-connector-python-rf` package or its official counterpart `mysql-connector-python` is not installed in the active Python environment.fixEnsure the correct package is installed: `pip install mysql-connector-python-rf` (though highly discouraged) or `pip install mysql-connector-python` (recommended). -
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'your_user'@'localhost' (using password: YES)
cause Incorrect database user credentials (username or password) or insufficient privileges for the specified user.fixVerify the username, password, and host parameters in `mysql.connector.connect()`. Ensure the MySQL user has the necessary permissions on the target database. -
mysql.connector.errors.InterfaceError: 2003 (HY000): Can't connect to MySQL server on 'localhost:3306' (10061)
cause The MySQL server is not running, is not accessible from the client machine, or the connection parameters (host, port) are incorrect.fixCheck if the MySQL server is running and accessible. Verify the `host` and `port` parameters in the connection string. Ensure no firewall is blocking the connection. -
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual...
cause The SQL query being executed contains a syntax error or is malformed.fixCarefully review the SQL string passed to `cursor.execute()` for typos, incorrect keywords, or missing clauses. Use parameterized queries (`%s` placeholders) to prevent SQL injection and properly handle special characters.
Warnings
- breaking This library (`mysql-connector-python-rf`) is *abandoned and unmaintained*. Its last release was in February 2017, making it incompatible with modern Python versions and potentially containing unpatched security vulnerabilities.
- deprecated The package `mysql-connector-python-rf` was a temporary repackaging to resolve PyPI distribution issues of the official connector. It is severely outdated and not compatible with Python versions beyond 3.3.
- gotcha Forgetting to call `connection.commit()` after executing DML (INSERT, UPDATE, DELETE) statements will result in changes not being saved to the database. This is a common DB-API 2.0 footgun.
Install
-
pip install mysql-connector-python-rf
Imports
- mysql.connector
import mysql.connector
- MySQLConnection
from mysql.connector import MySQLConnection
Quickstart
import mysql.connector
import os
host = os.environ.get('MYSQL_HOST', 'localhost')
user = os.environ.get('MYSQL_USER', 'your_user')
password = os.environ.get('MYSQL_PASSWORD', 'your_password')
database = os.environ.get('MYSQL_DATABASE', 'test_db')
try:
# Establish a connection
cnx = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
if cnx.is_connected():
print("Successfully connected to MySQL database.")
cursor = cnx.cursor()
# Create a table (if it doesn't exist)
cursor.execute("CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), email VARCHAR(255))")
cnx.commit()
print("Table 'users' ensured to exist.")
# Insert data
add_user = ("INSERT INTO users (name, email) VALUES (%s, %s)")
data_user = ("John Doe", "john.doe@example.com")
cursor.execute(add_user, data_user)
cnx.commit()
print("Data inserted.")
# Query data
cursor.execute("SELECT id, name, email FROM users")
for (id, name, email) in cursor:
print(f"ID: {id}, Name: {name}, Email: {email}")
else:
print("Failed to connect to MySQL database.")
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
if 'cnx' in locals() and cnx.is_connected():
cursor.close()
cnx.close()
print("MySQL connection closed.")