Skip to content

Successive SQL operations on two encrypted databases are resulting in weird behavior #13

Open
@HydroCarbons

Description

@HydroCarbons

Successive SQL operations (querying and creating, inserting & querying) two encrypted databases are resulting in weird behavior

PACKAGE.JSON

{
  "name": "multi-db-issue-with-sqlite",
  "version": "1.0.0",
  "description": "",
  "main": "multi-db-issue.js",
  "keywords": [],
  "author": "HydroCarbons",
  "license": "ISC",
  "dependencies": {
    "sqlite-cipher": "^0.3.5"
  }
}

CODE (multi-db-issue.js)

const fs = require('fs');
var sqlite = require('sqlite-cipher');

function simulateWorkflow(databaseFilePath, databasePassword, fcbOnCompletion) {

  var bFileExist = false;
  if (fs.existsSync(databaseFilePath)) {
    bFileExist = true;
    console.log(databaseFilePath + " exist");
  }

  var c;
  c = sqlite.connect(databaseFilePath, databasePassword, 'aes-256-ctr');

  if(bFileExist == false ) {
    console.log(databaseFilePath + " is a new database");
    try {
        var createData = c.run( "CREATE TABLE `_internal` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `client_name` TEXT UNIQUE ); ");
        console.log("Creation : " + JSON.stringify(createData));
        if(createData.error) {
          return;
        }
        var insertData = c.run( "INSERT INTO `_internal` (`client_name`) VALUES (\'" + databaseFilePath + "\'); ");
        console.log("Insertion : " + JSON.stringify(insertData));
        if(insertData.error) {
          return;
        }
        var rows = c.run( "SELECT * FROM `_internal`");
        console.log("DB(" + databaseFilePath + ") query result => " + JSON.stringify(rows) );
        c.close();
        c = null;
    } catch(e1) {
      console.log("SQL Exception : " + e1);
      c.close();
      c = null;
    }

  }
  else {
  // Query contents of _internal table in case DB file is present
    try {
      var rows = c.run( "SELECT * FROM `_internal`");
      console.log("DB(" + databaseFilePath + ") query result => " + JSON.stringify(rows) );
      c.close();
      c = null;
    } catch(e1) {
      console.log("SQL Exception : " + e1);
      c.close();
      c = null;
    }
  }
}
//
// Precondition: x1.db (password: 'pass') is already present with '_internal' db and one entry (client_name = x1)
//
simulateWorkflow('x1.db', 'pass');
simulateWorkflow('x2.db', '4_jS3XEW{D#E##(@!JD');

OUTPUT

[First execution]

  **HydroCarbons**  

 node multi-db-issue.js 
x1.db exist
DB(x1.db) query result => [{"id":1,"client_name":"x1.db"}]
x2.db is a new database
Creation : {"error":{}}

[Expected]

  • No errors in creating DB

[Second execution]

  **HydroCarbons**  

 node multi-db-issue.js 
x1.db exist
DB(x1.db) query result => [{"id":1,"client_name":"x1.db"}]
x2.db exist
DB(x2.db) query result => [{"id":1,"client_name":"x1.db"}]

[Expected]

  • DB(x2.db) query result => [{"id":1,"client_name":"x2.db"}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions