I am trying to execute an MYSQL insert query using an executeCommandLine statement and I am not able to figure out where I am going wrong. I have read various comments stating that the spaces should be replaced by @@. Then I read another that the executeCommandLine has changed where the @@ are no longer used and the string should be split up anywhere there is a pace.
Below is the rule I am trying to run. The query works with no issue on a terminal.
var mysql_host = "localhost"
var mysql_user = "ohuser"
var mysql_pw = "supersecretpassword"
var mysql_db = "databasename"
rule "Write to database"
when
Item monitoredItem changed
then
var latitude = Xlatitude.state as Number
var longitude = Xlongitude.state as Number
var accuracy = Xaccuracy.state.toString().replace(" ft", "")
var DateTimeType lastSeen = XlastSeen.state.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS").toString
var battery = Xbattery.state as Number
var icon = "mylocation"
var nowvalue = lastSeen + "'," + latitude + "," + longitude + ",'" + accuracy + "','" + icon + "', " + battery
var nowvaluestring = nowvalue.toString()
//var String sql_query = "/usr/bin/mysql -h " + mysql_host + " -u " + mysql_user + " -p" + mysql_pw + " -e \"INSERT INTO Tracker (last_seen,latitude,longitude,accuracy,icon,battery) VALUES (" + nowvaluestring + ");\" -D " + mysql_db
var String sql_query = "/usr/bin/mysql@@-h@@" + mysql_host + "@@-u@@" + mysql_user + "@@-p" + mysql_pw + "@@-e@@INSERT INTO Tracker (last_seen, latitude, longitude, accuracy, icon, battery) VALUES ('" + nowvaluestring + ");@@" + mysql_db
var String mysql = executeCommandLine(sql_query)
if( mysql!="") {
//logInfo("SQL","SQL query to be executed: " + sql_query)
logInfo("SQL","Result SQL query: " + mysql)
}else{
logInfo("SQL","Success: " + mysql )
}
end
Finally, I saw another https://community.openhab.org/t/bmw-connectdrive-bypass-mariadb-persistence-using-execcommandline-fails-with-error-36-filename-too-long/122541/6 using the following setup which to me is a bit more elegant and easier to read but I am getting an error saying there is no suitable driver but not really sure what to do about it.
var String sql_query = "INSERT INTO GPSTracker (last_seen, latitude, longitude, accuracy,
icon, battery) VALUES (" +nowvaluestring + ");"
var conn = java.sql.DriverManager.getConnection( "jdbc:mariadb://127.0.0.1:3306/ohdb?useSSL=false&serverTimezone=US/Eastern", "username", "password" );
var stmt = conn.createStatement();
var rs = stmt.executeQuery(sql_query)
I am using the Mariadb persistence via JDBC and that is working as expected so I am at a loss.
internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘mysql-1’ failed: No suitable driver found for jdbc:mariadb: