On 03/04/2017 23:08, Clemens Ladisch wrote:
I think you can work around these problems by moving the blob value into
a separate table.


I thought so too. However, even updates to other tables invalidate the blob handle. Please see the example below.

Variable breakMe breaks the second sqlite3_blob_write call, even though the added update statement is for the other table.


Yuri


#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"

int breakMe = 1;

char *exec_errmsg;

void execSql(sqlite3 *db, const char *sql) {
  int rc;
if (SQLITE_OK != (rc = sqlite3_exec(db, sql, NULL, NULL, &exec_errmsg))) {
    fprintf(stderr, "Error while executing sql=%s, error=%d\n", sql, rc);
    exit(1);
  }
}

int main(int argc, char **argv) {
  sqlite3 *db = NULL;
  int rc;

  rc = sqlite3_open(argv[1], &db);
  if (SQLITE_OK != rc) {
fprintf(stderr, "Can't open database %s (%i): %s\n", argv[1], rc, sqlite3_errmsg(db));
    exit(1);
  }

execSql(db, "CREATE TABLE fragmented_data ( friend_id INTEGER NOT NULL, frags_id INTEGER NOT NULL, message BLOB, PRIMARY KEY ( friend_id, frags_id));"); execSql(db, "CREATE TABLE fragmented_meta ( friend_id INTEGER NOT NULL, frags_id INTEGER NOT NULL, timestamp_first INTEGER NOT NULL, timestamp_last INTEGER NOT NULL, frags_done INTEGER NOT NULL, frags_num INTEGER NOT NULL, message BLOB, PRIMARY KEY ( friend_id, frags_id));");

execSql(db, "INSERT INTO fragmented_data (friend_id, frags_id, message) SELECT 0, 1488703606591, zeroblob(2070) WHERE NOT EXISTS (SELECT 1 FROM fragmented_meta WHERE friend_id=0 AND frags_id=1488703606591);"); execSql(db, "INSERT INTO fragmented_meta (friend_id, frags_id, timestamp_first, timestamp_last, frags_done, frags_num) SELECT 0, 1488703606591, 1488703647556, 1488703647556, 0, 2 WHERE NOT EXISTS (SELECT 1 FROM fragmented_meta WHERE friend_id=0 AND frags_id=1488703606591);");

  sqlite3_blob *blob1;
rc = sqlite3_blob_open(db, "main", "fragmented_data", "message", 1, 1, &blob1);
  if (SQLITE_OK != rc) {
fprintf(stderr, "Couldn't get blob handle (%i): %s\n", rc, sqlite3_errmsg(db));
    exit(1);
  }
  if (SQLITE_OK != (rc = sqlite3_blob_write(blob1, "1", 1, 0x10))) {
    fprintf(stderr, "Error writing to blob handle #1. rc=%d\n", rc);
    exit(1);
  }

  if (breakMe)
execSql(db, "UPDATE fragmented_meta SET timestamp_last=max(timestamp_last,1488703647556), frags_done = frags_done+1 WHERE friend_id=0 AND frags_id=1488703606591;");

  if (SQLITE_OK != (rc = sqlite3_blob_write(blob1, "2", 1, 0x250))) {
    fprintf(stderr, "Error writing to blob handle #2. rc=%d\n", rc);
    exit(1);
  }

  sqlite3_blob_close(blob1);
  printf("Successfully wrote to blob\n");
  sqlite3_close(db);
  return 0;
}

_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to