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, so recently I've been reading into ninject but I am having trouble understanding
Okay, I've been reading about rvalues and they seem like a great idea, but
Okay, I've been working through a set of string replacments for bbcode style tags
Okay, I'm working on a problem that I've been holding off on for three
Okay, I'm probably missing something really simple here, but I've been at this for
Okay, so here's the context. I've been up for almost a day straight now
Okay, I've been reading and searching around, and am now banging my head against
I've been using Linq-to-SQL for quite awhile and it works great. However, lately I've
Okay, I've been struggling with this all weekend, and I've gotten plenty of help
Okay, I've been looking all over the web to find a solution but I

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.