0

i want to increment a column in my database , how can i do that ?

if result[0] is True and result[1] != "":  # a win
               your_details["score"] = your_details["score"] + 1
               lbl_status["text"] = "Game over, You won! You(" + str(your_details["score"]) + ") - " \
                                                                                              "" + opponent_details[
                                        "name"] + "(" + str(opponent_details["score"]) + ")"

               req = 'UPDATE INTO users("nb_win") VALUES("nb_win + 1")'
               infos = your_details["score"]
               print(infos)

               try:
                   cursor.execute(req, infos)
                   db_connection.commit()

               except mysql.Error as err:
                   print(err)
def connect():
    global your_details
    if len(ent_name.get()) < 1:
        tk.messagebox.showerror(title="ERROR!!!", message="You MUST enter your first name <e.g. John>")
    else:
        your_details["name"] = ent_name.get()
        print(ent_name.get())
        connect_to_server(ent_name.get())

        req = 'INSERT INTO users(user_name) VALUES("'+ ent_name.get()+'")'
        infos = ent_name     

If someone can help me, thanks for your help !

1 Answer 1

1

You want an update statement. Assuming that the primary key of your table is id, that would be:

UPDATE users SET nb_win = nb_win + 1 WHERE user_name = ?

The ? in the query should be replaced with the id of the row whose nb_win you want to increment.

Sign up to request clarification or add additional context in comments.

3 Comments

I try to add req = 'UPDATE users SET nb_win = nb_win + 1 WHERE id = ("'+your_details["name"]+'")' , but didn't work. I update my post with where I add the username of the user but it's in a function. I don't know if i can retrieve that.
@nico31780: maybe you want req = "UPDATE users SET nb_win = nb_win + 1 WHERE user_name = '" + your_details["name"] + "'"?
Thank you it worked ! You can update your answer so i can accept it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.