It looks like MySQLdb, psycopg2 and cx_Oracle have pretty simillar interfaces for coneccting to databases. So it sounds reasonable to create a factory that someone can use like this:
conn = DBDriverFactory("MySQL", connectionDetails)
But first I want to ensure that:
- I’m not missing some better solution and my idea is not really stupid.
- There isn’t any well known working solution like that to prevent from reinventing a wheel.
I know django and alchemy should do these things but I’m looking for something very lightweight and simple (but efficient and elegant).
The reason this doesn’t exist as a “lightweight and simple” solution is that most SQL database engines use different SQL syntax, different DB-API parameterization and sometimes subtly different semantics, so changing database is not as simple as changing the DB-API module to use. A real abstraction would require parsing the SQL and generating new SQL appropriate for the database in use.
I don’t know of any package that tries to do this, as the common approach (and much easier) is to generate engine-specific SQL from a more easily parsable form (like the classes ORM’s like SQLAlchemy and Django’s ORM have you create.)