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

  • Home
  • SEARCH
  • 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 1009287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:54:07+00:00 2026-05-16T08:54:07+00:00

I’m sure I can check if a row exists by selecting it but I’m

  • 0

I’m sure I can check if a row exists by selecting it but I’m wondering if there’s a slicker way that I’m just not aware of — seems like a common enough task that there might be. This SQLite table looks something like this:

rowID  QID    ANID  value
------ ------ ----- ------
0      axo    1     45
1      axo    2     12

If the combination of QID and ANID already exists, that value should be updated, if the combination of QID and ANID doesn’t already exist then it should be inserted. While its simple enough to write:

SELECT * where QID = 'axo' and ANID = 3;

And check if the row exists then branch and either insert/update I can’t help but look for a better way. Thanks in advance!

  • 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-16T08:54:07+00:00Added an answer on May 16, 2026 at 8:54 am

    The insert documentation details a REPLACE option

    INSERT OR REPLACE INTO tabname (QID,ANID,value) VALUES ('axo',3,45)
    

    The “INSERT OR REPLACE” can abbreviated to REPLACE.

    Example in Perl

    Revised the following example to utilize a composite primary key

    use strict;
    use DBI;
    
    ### Connect to the database via DBI
    my $dbfile = "simple.db";
    unlink $dbfile;
    my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile");
    
    ### Create a table
    $dbh->do("CREATE TABLE tblData (qid TEXT, anid INTEGER, value INTEGER, PRIMARY KEY(qid, anid))");
    
    ### Add some data
    my $insert = $dbh->prepare("INSERT INTO tblData (qid,anid,value) VALUES (?,?,?)");
    
    $insert->execute('axo', 1, 45);
    $insert->execute('axo', 2, 12);
    
    $insert->finish;
    
    ### Update data
    my $insert_update = $dbh->prepare("REPLACE INTO tblData (qid,anid,value) VALUES (?,?,?)");
    
    $insert_update->execute('axo', 2, 500);
    $insert_update->execute('axo', 10, 500);
    
    $insert_update->finish;
    
    ### Print out the data
    my $select = $dbh->prepare("SELECT * FROM tblData ORDER BY 1,2");
    $select->execute;
    
    while (my @row = $select->fetchrow_array()) {
        printf "Row: %s\n", join(" - ", @row);
    }
    
    $select->finish;
    
    $dbh->disconnect;
    exit 0;
    

    Produces the following output, demonstrating the update of one row and the insert of another

    Row: axo - 1 - 45
    Row: axo - 2 - 500
    Row: axo - 10 - 500
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.