【发布时间】:2019-12-27 20:46:36
【问题描述】:
我目前正在使用 Google 应用脚本开发数据提取插件。主要思想是应用程序的用户可以将数据从工作表插入数据库。为此,我使用的是应用程序脚本提供的 JDBC api
我目前遇到的问题是,当我从工作表中读取一个空的应用程序脚本的单元格时,它使用了未定义的类型,因此在插入时会产生错误。我怎么能做这种事?
我当前的插入函数:
function putData(row, tableName) {
var connectionName = '****';
var user = '****';
var userPwd = '*****';
var db = '******';
var dbUrl = 'jdbc:google:mysql://' + connectionName + '/' + db;
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
var stmt = conn.createStatement();
var data = row
var query = "INSERT INTO "+ db + '.' + tableName +" VALUES (" ;
var i = 0
//The following loop is just to build the query from the rows taken from the sheet
// if the value is a String I add quotation marks
for each (item in row){
if ((typeof item) == 'string'){
if (i == row.length-1){
query += "'" + item + "'";
} else {
query += "'" + item + "',";
}
}else {
if (i == row.length-1){
query += item;
} else {
query += item + ",";
}
}
i++
}
query += ")"
results = stmt.executeUpdate(query)
stmt.close();
conn.close();
}
当我在某些情况下尝试插入单词“NULL”时,我认为它是一个字符串并在其他字段上显示错误。
【问题讨论】:
-
我认为你可以测试它的真实性和类型。如果真实性为假,则插入 null。
标签: mysql sql google-apps-script jdbc google-sheets