Skip to content

Instantly share code, notes, and snippets.

@gguuss
Created October 31, 2019 12:22
Show Gist options
  • Save gguuss/0e646d0632d8799fcd8f39cb0b0498e0 to your computer and use it in GitHub Desktop.
Save gguuss/0e646d0632d8799fcd8f39cb0b0498e0 to your computer and use it in GitHub Desktop.
/*
* Whitelist the following IPs on https://cloud.google.com/sql
* One at a time!
*
* 64.18.0.0/20
* 64.233.160.0/19
* 66.102.0.0/20
* 66.249.80.0/20
* 72.14.192.0/18
* 74.125.0.0/16
* 173.194.0.0/16
* 207.126.144.0/20
* 209.85.128.0/17
* 216.239.32.0/19
*/
// Replace the variables in this block with real values.
var address = '<your-sql-ip-address>';
var user = 'root';
var userPwd = 'your-password';
var db = 'your-db';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
// Read up to 1000 rows of data from the table and log them.
function readFromTable() {
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var start = new Date();
var stmt = conn.createStatement();
stmt.setMaxRows(1000);
var results = stmt.executeQuery('SELECT * FROM wifistr');
var numCols = results.getMetaData().getColumnCount();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow(["ID", "Time", "Wifi Strength"]);
while (results.next()) {
var rowArr = [];
for (var col = 0; col < numCols; col++) {
res = results.getString(col + 1);
if (col == 2) {
res=res.replace('Wifi: ',''); // can even hackily clean up db
res=res.replace('db','');
}
rowArr.push(res);
}
sheet.appendRow(rowArr);
}
results.close();
stmt.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment