Append string in the middle of a query in python -
i'm trying build query in python, i'm looking elegant way append condition in middle of string:
def get_raw_queryset(frequency=none, where_condition=none): qs = "select id, count(*) count user_transaction_log " \ # want append if not none if where_condition: "where .... = 1" \ "group type , strftime('{0}', datetime) order id" \ .format(frequency) return qs
this work long can safely evaluate string if where_condition
not string:
"select ..." + bool(where_condition) * ("where ...") + "group ..."
i hope you're being extremely careful avoid sql injection.
Comments
Post a Comment