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 3974124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:31:53+00:00 2026-05-20T04:31:53+00:00

Okay so ive been reading up on and working with SQLite recently (someone on

  • 0

Okay so ive been reading up on and working with SQLite recently (someone on here suggested it actually).

It works great for what I need – an in-memory database. The problem I am having however is that their are duplicate records in here so what I wanted to do was insert the record but if it already exists then just fill in the missing fields.

Now the first method I tried was setting the id as a primary key and INSERT OR REPLACE into. The issue with this was the previous contents were being wiped out.

So what im doing now is an update query. Im them checking the number of rows changed. if its below 1 then I run the insert query (if there a better way do share as I know this has extra overhead).

Anyway my issue (finally).. is that even with the update query the records are being overwritten with null values.

Ive condensed the snippets of code below:

    $stmt1 = $db->prepare("UPDATE results SET 
        field1=?, field2=?, field3=? 
        WHERE id=?
    ");

    $stmt1->execute(array(
        $elm['content1'], $elm['content2'], $elm['content3'], $elm['id']
    ));

    $count = $stmt1->rowCount();

    if ($count < 1) {
        $stmt2 = $db->prepare("INSERT INTO results (id, field1, field2, field3) 
            VALUES (:id,:field1, :field1, :field1 )
        ");

        $stmt2->execute(array(":id" => $recordID, ":field1" => $elm['content1'], ":field2" => $elm['content2'], ":field3" => $elm['content3']));    
    }

The above is going on within a foreach loop.

Is their anyway to stop the content being overwritten if its already in the db. So if on the update the field is null then add the new content, if its already got something in it leave it unaltered and move on to the next field. Ive read about an IFNULL which can be used in SQLite but im not sure how I use that within my PDO prepared statements.

Any help, guidance, examples would be appreciated 🙂

p.s Im using PHP5 with SQLite

  • 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-20T04:31:53+00:00Added an answer on May 20, 2026 at 4:31 am

    if on the update the field is null then add the new content, if its already got something in it leave it unaltered and move on to the next field.

    You can use coalesce to do this; here is an example:

    ~ e$ sqlite3
    SQLite version 3.7.5
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> create table foo (a,b,c);
    sqlite> insert into foo values (1,NULL,3);
    sqlite> select * from foo;
    1||3
    sqlite> update foo set a = a + 1, b = coalesce(b,'b'), c = coalesce(c,'c');
    sqlite> select * from foo;
    2|b|3
    sqlite> 
    

    So, your stmt1 would be:

    $stmt1 = $db->prepare("UPDATE results SET 
        field1=coalesce(field1,?), field2=coalesce(field2,?), field3=coalesce(field3,?)
        WHERE id=?
    ");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I've been reading about rvalues and they seem like a great idea, but
Okay, I've been reading and searching around, and am now banging my head against
I've been reading up on lex/yacc. The books and examples are not hard to
I've been asking questions on hex and bitwise manipulation (here and elsewhere) for the
Okay so I've set up and tested a nice little WCF service. The client
Greetings! I've been fooling around (a bit) with C# and its assemblies. And so
I've been using Netbeans 6.9 quite happily with Android for a few months but
How do Services and Repositories relate to each other in DDD? I mean, I've
I have a few questions about divide overflow errors on x86 or x86_64 architecture.

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.