3

In the react-native-sqlite-storage's tests (see https://github.com/andpor/react-native-sqlite-storage/blob/master/test/index.ios.promise.js#L141-L149) I find:

tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Sylvester Stallone", 2,  4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Elvis Presley", 2, 4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Leslie Nelson", 3,  4);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Fidel Castro", 3, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Bill Clinton", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Margaret Thatcher", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Donald Trump", 1, 3);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Dr DRE", 2, 2);');
tx.executeSql('INSERT INTO Employees (name, office, department) VALUES ("Samantha Fox", 2, 1);');

Is there a way to do this with one statement by passing an array?

1 Answer 1

8

Assuming that myDb is your connection variable :

const myDb=SQLite.openDatabase({name:"yourDb", createFromLocation:"yourDB.db"}, ()=>console.log('DB opened'), (err)=>alert('error : ' + err));

If you want to insert three rows into your "Employees" table :

myDb.transaction((tx)=>{
  tx.executeSql(        
    'INSERT INTO Employees (name,office,departement) VALUES (?,?,?),(?,?,?),(?,?,?)',
    ['Sylvester Stallone',2,4,'Elvis Presley',2,4,'Leslie Nelson',3,4],
    (tx, results) => {               
      if (results.rowsAffected > 0 ) {
        console.log('Insert success');              
      } else {
        console.log('Insert failed');
      }
    }
  );
});

Good luck!

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any limit on number of rows that can be inserted?
Honestly, @Kunal, i dont know. i haven't yet try inserting a very large amount of rows.

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.