by Scene_Cast2 · 3y ago · 0 comments
I found that there are just so many differences that supporting both leads to having multiple codepaths. Some examples:
- only postgres supports `with conn.cursor as cur`, need `cur.close()` or commit or similar with SQLite
- Escaping things for columns and table names: postgres has psycopg2.sql.SQL.format() that only works with postgres connections.
As my least favorite example, DB-API has two transaction modes, both of which are unnecessarily error prone: autocommit on, and autocommit off. Autocommit on makes transactions useless (intentionally), but it still allows commit() and rollback(), so one can write code that thinks transactions are enabled, have it appear to work, and end up with all manner of corruption.
Autocommit off has a bad interface. The begin() function is entirely missing, so you can’t pass it arguments. Worse, since there is no explicit start of a transaction, the library can’t catch misuse of a transaction.
As a result, every SQL library for Python of any reasonable quality implements its own special API, and nothing is particularly interoperable.
This is all sort of fine if the database is only accessed through a higher level library like SQLAlchemy. But sometimes a developer just wants to execute raw SQL against a database, and the situation in Python is not very nice.
I think the real problem is that DB-API exists and is bad — its existence comes with the automatic imprimatur of being an official standard and this seeming desirable to use.
(PostgreSQL experts: Is there a magic way to sync schema changes and data from a local instance to a production server?)