Jump to content

Python - sqlite vs. mysql

From Squishu Wiki

General

  • In Python, both MySQL and SQLite are accessed using modules that conform to the standard Python Database API Specification v2.0 (PEP 249), meaning the way you interact with them programmatically is very similar
  • sqlite3 module is built into the python standard library
  • mysql requires installing a separate Python connector (e.g., mysql-connector-python, PyMySQL)
  • For basic operations (connecting, creating cursors, executing queries, fetching results), the code in Python is nearly identical for both, thanks to the shared API standard
import sqlite3
import mysql.connector # or PyMySQL

# Connection (different connection string/parameters)
conn_sqlite = sqlite3.connect('my_database.db')
conn_mysql = mysql.connector.connect(host='localhost', user='user', password='pwd', database='my_db')

# Cursor creation (same method name)
cursor_sqlite = conn_sqlite.cursor()
cursor_mysql = conn_mysql.cursor()

# Executing queries (same method name)
cursor_sqlite.execute("SELECT * FROM users")
cursor_mysql.execute("SELECT * FROM users")

  • Connection Parameters: SQLite only needs a file path. MySQL needs host, username, password, etc., for the server connection
  • SQL Dialect: While the general SQL is the same, minor syntax differences exist (e.g., SQLite uses AUTOINCREMENT for auto-generating IDs, while MySQL uses AUTO_INCREMENT)
  • For flexibility, developers often use an Object-Relational Mapper (ORM) like SQLAlchemy which provides a high-level, database-agnostic interface, allowing you to switch the underlying database type (e.g., from SQLite during prototyping to MySQL in production) with minimal code changes

mysql vs. mariadb

  • Yes, Python handles MySQL and MariaDB almost identically due to MariaDB being designed as a binary drop-in replacement for MySQL. Both use the same client protocol, allowing the same Python connectors and APIs to work for either database server without modification in most cases
  • Same Connectors: You can use the same Python libraries (drivers) to connect to both databases. Popular choices include:
    • mysql-connector-python: The official Oracle-provided connector for MySQL, which also supports MariaDB
    • mariadb-connector-python: The official MariaDB-provided connector that fully implements the Python DB API 2.0 specification
    • Third-party libraries like PyMySQL, mysqlclient, and ORMs like SQLAlchemy also work seamlessly with both
  • Identical APIs: All the standard Python database APIs and structures are the same for both, following the PEP 249 specification. This means your code for connecting, executing queries, fetching results, and handling errors will be nearly identical
  • Binary Compatibility: The data files and client protocols are largely binary compatible, which means that Python applications often don't even "know" which underlying database they are connecting to; they just see a compatible MySQL interface
  • A Python developer can write code that seamlessly switches between a MySQL and a MariaDB backend with little to no code change.