S
S
stayHARD2016-04-05 21:45:20
Python
stayHARD, 2016-04-05 21:45:20

How to do INSERT OR UPDATE?

Hello.
You need to do something like INSERT OR UPDATE.
Essence:
There are records of the form:
name = string (example - John)
age = number (example - 24)
type = string, can be one of ["employee", "employer", "both"]
So, you need to make a request for adding data, first checking whether there are similar ones in the database and update if there is.
Algorithm:
Suppose the database already has:
John, 24, employee
1st case:
We are trying to put:
John, 24, employee
Result - do nothing
2nd case:
We are trying to put:
John, 24, employer
Result - update the object to John, 24 , both
That's actually all. Are there any considerations?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Marat, 2016-04-05
@Joysi75

Execute with 2 commands in the DBMS. 1) perform
select * from table where age=X and name=Y before your operation name) - parse the returned type and then perform the necessary update or not. I do not know the capabilities of sqlite - maybe you can make a stored procedure in it, where you can implement all the logic inside and call it with parameters.

I
Ivan Soshnikov, 2016-04-05
@soshnikov

To begin with, it would be good to designate the database and libraries that you use.
If it's a secret - then the most stupid and costly way: select-insert|update

A
Alexey Lebedev, 2016-04-06
@swanrnd

Do an UPDATE, if it returns 0 affected rows, then do an INSERT

I
igruschkafox, 2016-04-14
@igruschkafox

in MS SQL
Merge
in My SQL
Insert into .... with duplicate key Update
try both options :)
I don't know exactly which one will work in SQL Lite

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question