3

When inserting multiple rows to SQLite3 in PHP using a prepared statement, if you don't bind a parameter for a row then the value from the previous row will be inserted, even if you "clear" the statement between lines.

Look at the following example:

$db = new SQLite3('dogsDb.sqlite');

//create the database
$db->exec("CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age INTEGER)");    

$sth = $db->prepare("INSERT INTO Dogs (Breed, Name, Age)  VALUES (:breed,:name,:age)");

$sth->bindValue(':breed', 'canis', SQLITE3_TEXT);
$sth->bindValue(':name', 'jack', SQLITE3_TEXT);
$sth->bindValue(':age', 7, SQLITE3_INTEGER);
$sth->execute();

$sth->clear(); //this is supposed to clear bindings!
$sth->reset();

$sth->bindValue(':breed', 'russel', SQLITE3_TEXT);         
$sth->bindValue(':age', 3, SQLITE3_INTEGER);
$sth->execute();

Even though I would expect the second line to have a NULL value for the 'name' column, the value is 'jack' instead!

So either 'clear' doesn't seem to work (although it returns true) or I haven't really understood what it's supposed to do.

How can I clear the bindings between inserts in SQLite3 (or even PDO)? What's the best way to insert multiple rows where some rows might have null values for some fields?

symos
  • 31
  • 3
  • This just got confirmed as a PHP bug, see here: [https://bugs.php.net/bug.php?id=70628](https://bugs.php.net/bug.php?id=70628) – symos Jun 27 '16 at 14:57

1 Answers1

0
#include <sqlite3.h>
#include <stdio.h>

int main(void) {

    sqlite3 *db;
    char *err_msg = 0;
    sqlite3_stmt *res;
    sqlite3_stmt *res1;
    int rc = sqlite3_open("test.sqlite", &db);

    if (rc != SQLITE_OK) {

        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);

        return 1;
    }

    char *sql = "CREATE TABLE Dogs (Id INTEGER PRIMARY KEY, Breed TEXT, Name TEXT, Age TEXT)";
    rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
    if (rc == SQLITE_OK) {
        rc = sqlite3_step(res);

    }
    sqlite3_finalize(res);

    char *sql1 = "INSERT INTO Dogs (Breed, Name, Age)  VALUES (:breed,:name,:age);";

    rc = sqlite3_prepare_v2(db, sql1, -1, &res1, 0);


    if (rc == SQLITE_OK) {
        printf("%d\n", sqlite3_bind_text(res1, 1, "breed1", 6, SQLITE_STATIC));
        sqlite3_bind_text(res1, 2, "name1", 5, SQLITE_STATIC);
        sqlite3_bind_text(res1, 3, "age1", 4, SQLITE_STATIC);
        printf("%d\n", sqlite3_step(res1));
    } else {

        fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
    }
    sqlite3_reset(res1);
    sqlite3_clear_bindings(res1);
    printf("%d\n", sqlite3_bind_text(res1, 2, "name2", 5, SQLITE_STATIC));
    printf("%d\n", sqlite3_bind_text(res1, 3, "age2", 4, SQLITE_STATIC));

    printf("%d\n", sqlite3_step(res1));


    sqlite3_finalize(res1);
    sqlite3_close(db);

    return 0;
}
user993553
  • 1,077
  • 5
  • 12
  • Thanks for your response. If the equivalent code works in C, then it means that _probably_ this is a PHP bug. However I'm not sure it's related to the other bug you linked to. – symos Oct 03 '15 at 13:15