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.
Related
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 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();
}
Code listed below when I attempt to use this function inside my spreadsheet, I get the following error: Google Script Error - Exception: You do not have permission to call Jdbc.getCloudSqlConnection
any ideas on how to fix this? The connection is made successfully it seems but gets stuck at create table
var connectionName = 'cosmic-tenure-310821:us-central1:propertydata';
var rootPwd = "password";
var user = "root";
var userPwd = 'password';
var db = 'propertydata';
var root = 'root';
var instanceUrl = "jdbc:google:mysql://" + connectionName;
var dbUrl = instanceUrl + "/" + db;
function createTable() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
conn.createStatement().execute('CREATE TABLE myProperties(APN INT, APNObject MEDIUMBLOB)');
}
try with adding "oauthScopes"
"oauthScopes": ["https://www.googleapis.com/auth/gmail.addons.execute","https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/sqlservice", "https://www.googleapis.com/auth/sqlservice.admin", "https://www.googleapis.com/auth/gmail.readonly", "https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/gmail.addons.current.action.compose"]
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.