1

I'm trying to use react-native-sqlite-storage to persist data on the device.

But I'll need to use a lot of transactions during my code and when I try to get my instance returned by SQLite.openConnection and open a transaction doesn't work. And if I try to executeSql from my db instace it works perfectly.

db if my instance that come from SQLite.openConnection()

// THIS WORKS
db.executeSql(queryCreateTable)
  .catch(erro => console.log(`erro executesql ${erro}`));

db.executeSql('INSERT INTO PEOPLE VALUES ("Mateus Pereira", 20)')
  .catch(erro => console.log(`erro executesql ${erro}`));

db.executeSql('SELECT * FROM PEOPLE').then(([results]) => {
  const { rows } = results;
  console.log(rows);
  for (let i = 0; i < rows.length; i += 1) {
    const row = rows.item(i);
    console.log(`Name: ${row.nome}, Idade: ${row.idade}`);
  }
});

// THIS DOESNT WORK
db.transaction().then((tx) => {
  tx.executeSql(queryCreateTable)
    .catch(erro => console.log(`erro executesql ${erro}`));

  tx.executeSql('INSERT INTO PEOPLE VALUES ("Mateus Pereira", 20)')
    .catch(erro => console.log(`erro executesql ${erro}`));

  tx.executeSql('SELECT * FROM PEOPLE').then(([results]) => {
    const { rows } = results;
    console.log(rows);
    for (let i = 0; i < rows.length; i += 1) {
      const row = rows.item(i);
      console.log(`Name: ${row.nome}, Idade: ${row.idade}`);
    }
  });
}).catch(error => console.log(error));


{
    "db": {
        "openargs": {
            "name": "Test2.db",
            "dblocation": "nosync"
        },
        "dbname": "Test2.db"
    },
    "txlock": true,
    "readOnly": false,
    "executes": []
}

1 Answer 1

3

Here is how to use a transaction:

db.transaction(tx => {
  tx.executeSql(`SELECT ...`)
  .then([tx2, results]) => {

  }
  .catch((error) => {
    console.log(error);
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I forgot to answer my own question when I realized how to do. Thankss @Henrik

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.