I have a google apps script like the following:
// Initialize DB variables
var address = 'my-address'
var user = 'my-user-name'
var password = 'my-password'
var db = 'my-db'
var dbUrl = 'jdbc:mysql://' + address + '/' + db
function testUtf8() {
// Generate an SQL query
var conn = Jdbc.getConnection(dbUrl, user, password)
var stmt = conn.createStatement();
var results = stmt.executeQuery('SELECT * FROM my_table WHERE col_name=\'中文\';')
// Display query result
var colCount = results.getMetaData().getColumnCount()
while (results.next()) {
var colCount = results.getMetaData().getColumnCount() // No result is retured even though there is a value of `中文` in col_name.
for (var col = 0; col < colCount; col++) {
Logger.log(results.getString(col + 1))
}
}
// Clean up
stmt.close();
conn.close();
}
The SQL query fails to find the data even if I have a value of 中文
in col_name
in the table.
When I change the query from SELECT * FROM my_table WHERE col_name='中文';
to SELECT * FROM my_table WHERE col_name='abc';
, it successfully returns the data.
So this must be a problem of encoding.
How can I successfully execute an SQL query which contains Chinese character?