I use the following C code to test sqlite3_scrub_backup() [1]. Unfortunately, it results in a malformed database disk image.

Also, I am surprised that VACUUMing the original database produces a smaller file than sqlite3_scrub_backup(). Should they not be the same size?

Is this a problem with scrub.c or with my code?


Ralf

[1] http://www.sqlite.org/src/artifact?ci=trunk&filename=ext/misc/scrub.c

-------------------------------------------------------

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

sqlite3 *db;

static void check(int r, int e) {
  if (r != e) {
    printf ("ERROR %d %s\n", e, sqlite3_errmsg(db));
  }
}

static int callback (void *user, int nCol, char **r, char **c) {
  int i;
  for (i = 0; i < nCol; i++) {
    printf("%s ", r[i]);
  }
  printf("\n");
  return 0;
}

#define Test1Db "test1.db3"
#define Test2Db "test2.db3"

int main(void)
{
  char *zErrMsg = 0;

  check(0, remove (Test1Db));
  check(SQLITE_OK, sqlite3_open (Test1Db, &db));

  check(SQLITE_OK, sqlite3_exec(db,
    "DROP TABLE IF EXISTS t;"
    "CREATE TABLE t(a, b, c);"
    "WITH r(i) AS ("
    "  SELECT 1 UNION ALL SELECT i+1 FROM r WHERE i<1000"
    ")"
    "INSERT INTO t (rowid, a, b, c)"
    "  SELECT i,zeroblob(100),zeroblob(100),zeroblob(100) FROM r;"
    "SELECT count() FROM t;",
    callback, NULL, NULL));

  check(SQLITE_OK, sqlite3_exec(db,
    "DELETE FROM t WHERE rowid > 500;"
    "SELECT count() FROM t;",
    callback, NULL, NULL));

  check(SQLITE_OK, sqlite3_close(db));

  check(0, remove (Test2Db));
  check(SQLITE_OK, sqlite3_scrub_backup(
    Test1Db, // Source database filename
    Test2Db, // Destination database filename
    &zErrMsg ));// Write error message here
  if (zErrMsg) {
    printf ("%s", zErrMsg);
    sqlite3_free(zErrMsg);
  }

  /* VACUUM database 1. */
  check(SQLITE_OK, sqlite3_open (Test1Db, &db));
  check(SQLITE_OK, sqlite3_exec(db,
    "VACUUM",
    callback, NULL, NULL));
  check(SQLITE_OK, sqlite3_close(db));

  /* Integrity-check database 2. */
  check(SQLITE_OK, sqlite3_open (Test2Db, &db));
  check(SQLITE_OK, sqlite3_exec(db,
    "PRAGMA integrity_check;"
    "SELECT count() FROM t;",
    callback, NULL, NULL));
  check(SQLITE_OK, 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