I am trying to use the JDBC Services of Google App Scripts to connect a Google Sheet to a local implementation I have on my computer of SQL Server (hosted on 127.0.0.1:3306).
I tried the following script :
// Replace the variables in this block with real values.
var address = 'database_IP_address';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_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 entries');
var numCols = results.getMetaData().getColumnCount();
while (results.next()) {
var rowString = '';
for (var col = 0; col < numCols; col++) {
rowString += results.getString(col + 1) + '\t';
}
Logger.log(rowString)
}
results.close();
stmt.close();
var end = new Date();
Logger.log('Time elapsed: %sms', end - start);
}
with address = localhost or 127.0.0.1
user/password : from a specific user I created with all access to db and able to access from anywhere.
database: my database name
This is returning me the following error :
Exception: Failed to establish a database connection. Check connection string, username and password.
Tried looking at similar problems but could not find something about LOCALHOST sql server.
Saw some problematics about allowing IPs to reach my database, but I am not sure how does this applies in my case since it is hosted on my local computer and I just want to make a demo for the moment so it will stay that way.
Note : I created my sql-server through MySQL Workbench/MySQL Server
How can I proceed ?
I've had this error several times and randomly "Exception: Failed to establish a database connection. Check connection string, username and password."
I found this and this issues opened in google issue tracker, thought it could be helpful for someone finding this exact question someday.
In my case the problem was the MS SQL version that was not supporting TLS 1.2. An updated version fixed the problem.
Related
I am trying to build the simple code to update a row in database using Google APP Script.
code executing without any errors but row in database is not getting updated. I am not sure where the code is going wrong.
var Database_Host = 'xxx.com'
var Database_Name = 'DB'
var Database_username = 'DB'
var Database_password = 'Password'
var Port_number = '306' //DB details are hidden
function DB_Update(){
var url = 'jdbc:mysql://' + Database_Host + ':' + Port_number + '/' + Database_Name
Logger.log(url)
var conn = Jdbc.getConnection(url, Database_username, Database_password);
Logger.log(conn);
conn.setAutoCommit(false);
var stmt = conn.createStatement();
var results = stmt.executeUpdate('update time set flag = 0 where id in (13671)');
stmt.executeUpdate(results);
conn.commit();
conn.setAutoCommit(true);
stmt.close();
conn.close();
}
Any help is appretiated.
posting answer so that others can check and use.
Problem I am facing is I am connected to dev database and checking the prod database for updates, this issue is because both dev and prod share same user name and password.
DB details are least checked incase of errors, hence posting answer so that other can check if they forgot to check the connection details.
I have a question. I would like to connect Apps Script to my Wordpress MySQL database but have some issue. Code bellow return error "ReferenceError: Lib is not defined". How can I fix it? My goal is to execute SQL queries on schedule time via Apps Script and past it down to spreadsheet. What is wrong with that code?
var server = "***"
var dbName = "***"
var username = "***"
var password = "***"
var port = ***
function createConnection(){
var url = "jbdc:mysql://" + server + ":" + port + "/" + dbName;
var conn = Lib.getConnection(Jdbc).getConnection(url, username, password);
var stmt = conn.createStatement();
var rs = stmt.execute("SELECT * FROM wp_post LIMIT 100")
console.log(rs)
conn.close();
}
I'm trying to connect to my MySQL server in Google Apps Script. I am able to connect through MySQL Workbench from my PC and with MySQL from another server, I tested permissions, I can insert and other stuff.
This is the code I have in Apps Script:
function myFunction() {
var server = "zikovi.eu";
var port = 3306;
var username = "test";
var database = "testdb";
var password = "testpwd";
var url = "jdbc:mysql://"+server+":"+port+"/"+database;
Logger.log(url);
var conn = Jdbc.getConnection(url, username, password);
No matter what, the error says to check the URL, username or password. The parameters are correct (you can check, it's a test user and db).
I can connect remotely with navicat, no problem, but when i'm trying to do that in google app script, I get the next error: Failed to establish a database connection. Check connection string, username and password.
I use the following function to connect:
function getConnection(){
var address = 'url.domain';
var user = 'username';
var userPwd = 'pass';
var db = 'database_name';
var spreadsheetURL = 'spreadsheetURL';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
return conn;
}
The inserted data is correct;
I tried with a password with no special characters.
The spreadsheet is shareable, anyone with the link can edit.
Make sure to whitelist your CIDR IP address ranges as per this documentation:
https://developers.google.com/apps-script/guides/jdbc#creating_google_cloud_sql_connections
Also, Google recommends using the Jdbc.getCloudSqlConnection(url) method instead if you wish to not have to whitelist IP addresses.
I am hoping someone can assist me in getting the JDBC script Reading from a Database example here... https://developers.google.com/apps-script/jdbc ...to work with my db.
function foo() {
var conn = Jdbc.getConnection("jdbc:mysql://<host>:3306/<instance>", "user", "password");
var stmt = conn.createStatement();
stmt.setMaxRows(100);
var start = new Date();
var rs = stmt.executeQuery("select * from clients");
var doc = SpreadsheetApp.getActiveSpreadsheet();
var cell = doc.getRange('a1');
var row = 0;
while(rs.next()) {
cell.offset(row, 0).setValue(rs.getString(1));
cell.offset(row, 1).setValue(rs.getString(2));
cell.offset(row, 2).setValue(rs.getString(3));
cell.offset(row, 3).setValue(rs.getString(4));
row++;
}
rs.close();
stmt.close();
conn.close();
var end = new Date();
Logger.log("time took: " + (end.getTime() - start.getTime()));
}
I plugged in my server and user info and changed the select * statement to the table "clients" in my db. Connection is successful but I get the following error...
TypeError: Cannot call method "getRange" of null. (line 9)
The table "clients" in my db has the following columns...
id, clientname, clientcontact, clientphone, clientnote
Can someone help me understand what needs to be changed in the example script for it to work with my clients table? I am not a programmer by far... but I would like to get this working and use it as a template to get data from mysql into google spreadsheets so I can then publish the sheet on my google site.
Thanks!
This script is supposed to run from a spreadsheet container or at least to have access to a spreadsheet to write its results.
So you have 2 possibilities:
Copy /paste your script in the spreadsheet script editor and run it from there.
Replace the spreadsheetApp.getActive with openbyId(spreadsheet id)
(See this other post for more information) your are indeed in almost the same situation since your script is not directly related to the spreadsheet it uses.