I'm having a problem with my JavaScript code, it says the error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1
I'm trying to call this query: 'SELECT * FROM wp_fsqm_direct_52'
It connects to the database fine and show the database data in the sheet when I call the query like this: var sql = 'SELECT * FROM wp_fsqm_direct_52';
But when I call the exact same query from a cell in the A1 cell in the sheet var sql = sheetData.getRange("A1").getValue();
, it returns me the error.
Sorry for my bad English, I'm from Venezuela, and this is my first time here.
This is my code:
function readFromTable() {
var ss = SpreadsheetApp.getActive();
var sheetDetails = ss.getSheetByName('Details');
var sheetData = ss.getSheetByName('Query Sheet');
var host = sheetDetails.getRange("B1").getValue();
var databaseName = sheetDetails.getRange("B2").getValue();
var userName = sheetDetails.getRange("B3").getValue();
var password = sheetDetails.getRange("B4").getValue();
var tableName = sheetDetails.getRange("B6").getValue();
var url = 'jdbc:google:mysql://' + host + '/' + databaseName;
Logger.log(url);
Logger.log(userName);
Logger.log(password);
var sql = sheetData.getRange("A1").getValue();
try {
var connection = Jdbc.getCloudSqlConnection(url, userName, password);
var results = connection.createStatement().executeQuery(sql);
var metaData = results.getMetaData();
var columns = metaData.getColumnCount();
// Retrieve metaData to a 2D array
var values = [];
var value = [];
var element = '';
// Get table headers
for (i = 1; i <= columns; i++) {
element = metaData.getColumnLabel(i);
value.push(element);
}
values.push(value);
// Get table data row by row
while (results.next()) {
value = [];
for (i = 1; i <= columns; i++) {
element = results.getString(i);
value.push(element);
}
values.push(value);
}
// Cloese connection
results.close();
// Write data to sheet Data
SpreadsheetApp.flush();
sheetData.getRange(2, 1, values.length, value.length).setValues(values);
SpreadsheetApp.getActive().toast('Data has been updated.');
} catch (err) {
SpreadsheetApp.getActive().toast(err.message);
}
}
A1 should be plain text without quotes. You seem to have added extra quotes '
in A1.
SELECT * FROM wp_fsqm_direct_52
I find that running SQL on my website's database that it likes me put backticks around field names and single quotes around strings that I supply. So here's an example that I just tried.
Unfortunately, the backticks are hard show so here because they invoke the code window so here is an image showing what I mean.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.