I have a method to delete a record from a specific table:
def del_record(self, id): Table1[id].delete_instance() Is it possible in this case to make it possible to delete a record from any table by passing for example its name as a parameter?
def add_item(self, id, modelname): modelname[id].delete_instance() In modelname, pass the name of the class of the model, in your case it is Table1 .
Then, can I manually create a SQL query to delete the record from the table?
DELETE FROM <Имя Таблицы> WHERE <Условие отбора записей> Such code will turn out:
def del_record(self, table_name, id): sql = "DELETE FROM {} WHERE id=?".format(table_name) db.execute_sql(query, (id,)) Ps.
The idea took from here .
Learn more about the db.execute_sql method (query, values) : http://docs.peewee-orm.com/en/latest/peewee/database.html?highlight=execute_sql
It was possible to substitute and id in the query:
sql = "DELETE FROM {} WHERE id={}".format(table_name, id)` But knowing about the sql-injections and not knowing where your id will come from, I decided to make the example more correct.
Source: https://ru.stackoverflow.com/questions/864825/
All Articles