I tried Apps Script JDBC connectivity to an external Database . It always says: failed to establish database connection . Did Google deprecated connectivity to external database server outside cloud ?
It's working like a charm. Check if you are using getConnection or getCloudSQLConnetion. The second one is to be used only with Cloud SQL.
The example below is working:
function doGet(){
return HtmlService.createTemplateFromFile('datatable').evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function getSql(){
var conn = Jdbc.getConnection('jdbc:mysql://sql4.freesqldatabase.com:3306/sql427377', 'sql427377', 'my_pass');
var stmt = conn.createStatement();
var results = stmt.executeQuery("select name, location, address, phone, fax from person");
Logger.log(results);
var resultsArray = [];
while(results.next()) {
resultsArray.push({'name': results.getString(1),
'location' : results.getString(2),
'address' : results.getString(3),
'phone' : results.getString(4),
'fax' : results.getString(5)});
}
return JSON.stringify(resultsArray);
}
Live version here.
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 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.
I am running the Google Script
function doCalc(p1,p2)
{
eval(UrlFetchApp.fetch('http://<domain>/<path>/appsscript.js').getContentText());
return appObject(p1,p2).resultA;
}
and I get the result when I run it in Google Sheets / App Scripts editor after deploying it
5:05:52 PM Notice Execution started
5:05:52 PM Error
Exception: You do not have permission to call Jdbc.getCloudSqlConnection. Required
permissions: https://www.googleapis.com/auth/sqlservice
appObject
revisedMarkup # code.gs:4
The appsscript.js code which runs in the Google Cloud Platform is
function appObject(p1,p2)
{
// Connnect GCP ......
var connectionName = '<connection name>';
var user = '<user>';
var userPwd = '<user passwd>';
var db = '<database name>';
var dbUrl = 'jdbc:google:mysql://' + connectionName + '/' + db;
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
// .... End Connnect GCP
var obj = {
resultA: <calculation 1>,
resultB: <calculation 2>
};
return obj;
}
Any idea how to solve the issue (i.e. the exception: You do not have permission...) above? I am a novice. I have given myself Cloud SQL Admin
and Owner permissions in the IAM in console.cloud.google.com.
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.