0

I would like to fill in a google spreadsheet cell with the output of a mysql database query, so I've written this simple script following the example in the google tutorial:

function get_tbs_number() {
    var conn = Jdbc.getConnection("jdbc:mysql://127.0.0.1:9008/icadata");
    var stmt = conn.createStatement();
    stmt.setMaxRows(100);
    var start = new Date();
    var rs = stmt.executeQuery("select count(*) from CNGSfiltered where run=10269 and (GTO1>4 or GTO2>4 or GTO3>4 or GTO4>4);");
    var doc = SpreadsheetApp.getActiveSpreadsheet();
    var cell = doc.getRange('a1');
    cell.offset(0, 0).setValue(rs.getString(1));
    rs.close();
    stmt.close();
    conn.close();
    var end = new Date();
    Logger.log("time took: " + (end.getTime() - start.getTime()));
}

The problem is that when I try to run the script a get an error connecting to the database, while the connection to the same database from command line (mysql -h localhost -P 9008 -u # -p# icadata) does work.

4

1 回答 1

0

问题是 Google 无法访问127.0.0.1. 尝试在路由器上打开 mysql 端口(9008)。然后使用 www.whatismyip.com 获取您的公共 IP 地址。

"jdbc:mysql://<myPublicIP>:9008/icadata"

然后,您可能必须运行包含谷歌 IP 的 sql grant 命令或使用 %(不是很安全)。

grant all on icadata.* to '<user>'@'<google ips>' identified by '<password>';

祝你好运!这里有一些参考:https ://developers.google.com/apps-script/jdbc

于 2012-08-01T18:04:29.053 回答