MRM
MRM

Reputation: 43

Insert into with condition

I'm working with python and using pymysql library and i want to write a query that insert an array in a line where a column has some special value. For example insert 'hi' into a column where user_id is 22 for that query i write this code from pymysql import * chat_id = 22 user_first_name = "hi"

db = connect(host="localhost", port=3306, user="root", passwd="", 
db='support',charset='utf8')
cursor = db.cursor()
cursor.execute("""INSERT INTO users user_firstname VALUE %s WHERE user_id is 
%s""",(user_first_name, chat_id))
db.commit()

how should i write this query in correct form?

Upvotes: 0

Views: 1361

Answers (2)

Eric
Eric

Reputation: 254

Francisco is right though. If you have a user_id already, then an UPDATE should be used to change the value of and existing record. The INSERT command, creates a new record.

Upvotes: 0

francisco sollima
francisco sollima

Reputation: 8338

If I'm undertanding, correctly, rather than an INSERT INTO, it seems you need an UPDATE:

cursor.execute("""UPDATE users SET user_firstname='%s' WHERE user_id=%s""",(user_first_name, chat_id))

Upvotes: 2

Related Questions