By using the 'addBatch' method we can collate each row of student data into a single SQL statement that can be used to connect to the database once and execute multiple times. In order to do this we use 'prepareStatement' to which we can then feed in our values
var stmt = db.prepareStatement("INSERT INTO students (StudentNo, FirstName, LastName, Shoesize) "As we gather data for the current row in our 'for loop' we can populate the above empty values through 'setObject' which requires the column and value to be provided. Then add this as a batch to the SQL statement.
+ "VALUES (?, ?, ?, ?)");
stmt.setObject(1, studentNo);Once all of the data has been gathered into batches we can 'executeBatch' to connect to the database and add them all in one go.
stmt.setObject(2, firstName);
stmt.setObject(3, lastName);
stmt.setObject(4, shoesize);
stmt.addBatch();
Batch add data to an SQL Table in Apps Script
God bless you.. You just saved my life
ReplyDelete