Reusing code for mysql and sqlite operations

I wrote a class which handles all sorts of sqlite operations.
Now that all is working I rewrote it for mysql which is pretty much exactly the same except for the query.

Ofcourse this isn’t right. I want to be able to use a config variable to determine which database to use.

I’m not sure how. Do I use a parent class that redirects the function calls. Do I try to wrap this with decorator functions. Do I need to look into inheritance.

Whats a good approach here?

Thanks!

I would suggest that you might want to think about using SQLalchemy - https://www.sqlalchemy.org/

It knows about lots of SQL databases and allows to write your code once and target lots of databases. SQLalchemy takes care of using the right features of each database to implement your intentions.

Then your config is going to be a database URL (if I remember correctly) to switch to different databases.

1 Like