If there are two or more conditions, how to use the replace command in SQLite

I want to update a row if it exists in the table, otherwise insert it into SQLite in a single query. From the SQLite documentation I found that we can use the REPLACE command to achieve this.

If there are two or more conditions, I would like to know how to use REPLACE:

Example:
If I have the following records in table TABLE1:

Name Type InitialValue FinalValue
A 1 20 40
B 2 23 50
A 3 40 60
C 3 54 70

The combination of name and type here will be unique.

I want to set initialvalue=50 and finalvalue=90, where name=A and Type=3 (if it exists), otherwise insert it.< /p>

I am using this command, but it gives this error:

REPLACE INTO table1 (Name,Type,InitialValue,FinalValue) VALUES
(‘A’,3,50,90) WHERE Name=’A’ AND Type = 3 ;

The error is:

near “WHERE”: syntax error Unable to execute statement

How can I achieve my goal? Please help.

replace is like insert, it just checks if there are duplicate keys, if it is
It will delete the row and insert a new row, otherwise it will only insert

If there is a unique index of (name, type) and type the following command, you can perform this operation< /p>

REPLACE INTO table1 (Name,Type,InitialValue,FinalValue) VALUES ('A',3,50,90 )

and already exists Name =’ A’and Type = 3 lines will be replaced

http://www.sqlite.org/lang_createindex.html

CREATE UNIQUE INDEX idx_name_type ON table1 (Name,Type)

Edit:
Quick note-REPLACE is always DELETES and then INSERT, so it is never a good idea to use it when reloading, because it requires an exclusive lock when deleting, and then When inserting

Some database engines have

INSERT ... ON DUPLICATE KEY UPDATE ...

sqlite 3 does not, But if the insertion fails, you can try try}) catch

http://blog.client9.com/2007/11/sqlite3-and-on-duplicate-key-update.html

SQLite UPSERT – ON DUPLICATE KEY UPDATE

I want to update a row if it exists in the table, otherwise insert it into SQLite in a single query. From SQLite Documentation I found that we can use the REPLACE command to achieve this goal.

If there are two or more conditions, I would like to know how to use REPLACE:

Example:< br>If I have the following records in table TABLE1:

Name Type InitialValue F inalValue
A 1 20 40
B 2 23 50
A 3 40 60
C 3 54 70

The combination of name and type will be unique here .

I want to set initialvalue=50 and finalvalue=90, where name=A and Type=3 (if it exists), otherwise insert it.

I am using this command, but It gives this error:

REPLACE INTO table1 (Name,Type,InitialValue,FinalValue) VALUES
(‘A’,3,50,90 ) WHERE Name=’A’ AND Type = 3 ;

The error is:

near “WHERE”: syntax error Unable to execute statement

How can I achieve my goal? Please help.

Replace is just like insert, it just checks if there is a duplicate key, and if it is, it will delete the row and insert New line, otherwise it will only insert

If there is a unique index of (name, type) and type the following command, you can perform this operation

REPLACE INTO table1 (Name,Type,InitialValue,FinalValue) VALUES ('A',3,50,90)

And the row with Name =’A’ and Type = 3 will be replaced

p>

http://www.sqlite.org/lang_createindex.html

CREATE UNIQUE INDEX idx_name_type ON table1(Name,Type)

Edit:
Quick note-REPLACE is always DELETES and then INSERT, so it is never a good idea to use it when overloading, because it requires an exclusive lock when deleting, and then when inserting

Some databases The engine has

INSERT ... ON DUPLICATE KEY UPDATE ...

sqlite 3 does not, but if the insertion fails, you can try try)) catch

http://blog.client9.com/2007/11/sqlite3-and-on-duplicate-key-update.html

SQLite UPSERT – ON DUPLICATE KEY UPDATE

Leave a Comment

Your email address will not be published.