I'm creating a basic database utility class in Python. I'm refactoring an old module into a class. I'm now working on an executeQuery()
function, and I'm unsure of whether to keep the old design or change it. Here are the 2 options:
- (The old design:) Have one generic
executeQuery
method that takes the query to execute and a booleancommit
parameter that indicates whether to commit (insert, update, delete) or not (select), and determines with an if statement whether to commit or to select and return. - (This is the way I'm used to, but that might be because you can't have a function that sometimes returns something and sometimes doesn't in the languages I've worked with:) Have 2 functions,
executeQuery
andexecuteUpdateQuery
(or something equivalent).executeQuery
will execute a simple query and return a result set, whileexecuteUpdateQuery
will make changes to the DB (insert, update, delete) and return nothing.
Is it accepted to use the first way? It seems unclear to me, but maybe it's more Pythonistic...? Python is very flexible, maybe I should take advantage of this feature that can't really be accomplished in this way in more strict languages...
And a second part of this question, unrelated to the main idea - what is the best way to return query results in Python? Using which function to query the database, in what format...?