Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:42:59+00:00 2026-05-27T12:42:59+00:00

I want to update a row if it exists in the table else insert

  • 0

I want to update a row if it exists in the table else insert it in SQLite in single query. From SQLite documentation I found out that we can use REPLACE command for achieving this.

I want to know how to use REPLACE if there are two or more conditions:

Example:
If I have table TABLE1 with following records:

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

Here Combination of Name and Type will be unique.

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

I am using this command but it’s giving this error:

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

Error is:

near “WHERE”: syntax error Unable to execute statement

How can I achieve my objective? Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:42:59+00:00Added an answer on May 27, 2026 at 12:42 pm

    replace is just like insert, it just checks if there is duplicate key and if it is
    it deletes the row, and inserts the new one, otherwise it just inserts

    you can do this if there is for example unique index of (Name,Type) and if you type the following command

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

    and there already exists a row with Name = ‘A’ and Type = 3 it will be replaced

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

    CREATE UNIQUE INDEX idx_name_type ON table1(Name,Type)
    

    EDIT:
    a quick note – REPLACE always DELETES and then INSERTs, so it is never a very good idea to use it in heavy load because it needs exclusive lock when it deletes, and then when it inserts

    some of the database engines have

    INSERT ... ON DUPLICATE KEY UPDATE ...
    

    sqlite 3 does not, but you can do try {} catch if insert fails

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

    SQLite UPSERT – ON DUPLICATE KEY UPDATE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have a table and insert data n a single row
Let's say I have a table and I want to insert a row. The
I want to always update the value of an update row in the database.
I want to update two columns in a table. The value of the second
i want to update my sqlite database but i cannot find the way to
I want to update a column in a table making a join on other
Hey what i want to do is update a table if the field exsits
Im trying to write an SQL query that will check in the table ‘persons’
I want to have a database table that keeps data with revision history (like
I have a table with 4 columns. I want to be able to INSERT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.