U
U
Ulukman Amangeldiev2020-06-19 19:35:21
MySQL
Ulukman Amangeldiev, 2020-06-19 19:35:21

How to change multiple variables at once?

Hello, there is a Sqlighter class that has methods for working with my database (create_table, get_value, etc.). Among them is the update_value() method:

def update_value(self, name, value, chat_id):
    """Обновляем значение переменной в таблице"""
    with self.connection:
        try:
            self.cursor.execute(f"UPDATE chats SET {name}={value} WHERE chat_id = {chat_id}")
        except:
            self.cursor.execute(f"UPDATE chats SET {name}='{value}' WHERE chat_id = {chat_id}")


which changes some value in the database.
I quite often have to reset several variables through this method.

db = Sqlighter()
db.update_value("i", i + 1, chat_id)
db.update_value("pressed_satisfied", "", chat_id)
db.update_value("pressed_not_satisfied", "", chat_id)
db.update_value("satisfied_players", 0, chat_id)
db.update_value("not_satisfied_players", 0, chat_id)
db.update_value("got_answer_pressed", 0, chat_id)
db.update_value("rate_question", 0, chat_id)


Is there any way to automate this so that I don't have to write multiple update_value() ?
Or maybe there is another way to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ayazer, 2020-06-19
@Atageldiev

well, update the method to update all the values ​​\u200b\u200bthat you need at once

UPDATE chats SET {name1}={value1}, {name2}={value2}, ...., {nameN}={valueN} WHERE chat_id = {chat_id}

https://dev.mysql.com/doc/refman/8.0/en/update.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question