Skip to content

Instantly share code, notes, and snippets.

@gguuss
Created November 7, 2019 20:36
Show Gist options
  • Save gguuss/a6cbdbbed682871170b2ac0e09291bbb to your computer and use it in GitHub Desktop.
Save gguuss/a6cbdbbed682871170b2ac0e09291bbb to your computer and use it in GitHub Desktop.
Example showing you how to retrieve rows for plotting SQL data from IoT core
// Replace the variables in this block with real values.
var address = '<your-server-ip-address>';
var user = 'root';
var userPwd = '<your-password>';
var db = '<your-db-name>';
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