The error states
Exception calling “ExecuteNonQuery” with “0” argument(s): “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 ’ ‘host_name’, ‘value1’, ‘value2’, ‘value3’, ’ at line 2” At C:\test.ps1:70 char:3 + $rowsInserted = $command.ExecuteNonQuery() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: ( , MethodInvocationException + FullyQualifiedErrorId : MySqlException
I connect using the following function:
function ConnectToMySQL([string]$user,[string]$pass,[string]$MySQLHost,[string]$database) {
[void][system.reflection.Assembly]::LoadWithPartialName(“MySql.Data”)
Open Connection
$connStr = “server=” + $MySQLserver + “;port=3306;uid=” + $user + “;pwd=” + $pass + “;database=”+$database+“;Pooling=FALSE”
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)
$conn.Open()
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand(“USE $database”, $conn)
return $conn
}
function WriteMySQLQuery($conn, [string]$query) {
$command = $conn.CreateCommand()
$command.CommandText = $query
$rowsInserted = $command.ExecuteNonQuery()
$command.Dispose()
if ($rowsInserted) {
return $rowInserted
} else {
return $false
}
}
$conn = ConnectToMySQL $user $pass $MySQLHost $database
$rows = WriteMySQLQuery $conn $query
On different forum it was recommended to use the following syntax for the query
$query=@"
INSERT INTO table(
‘host_name’,
‘value1’,
‘value2’,
‘value3’,
‘date_entered’,
‘host_id’
)
VALUE(
‘$host’,
‘$value1’,
‘$value2’,
‘$value3’,
now(),
Null
)
ON DUPLICATE KEY
UPDATE(
host_name=‘$host’,
value1=‘$value1’,
value2=‘$value2’,
value3=‘$value3’,
date_entered=now(),
host_id=Null
)
"@
Apart for looking much clearer (I see the indentation isn’t visible when I past it here) it also eliminated the ISE underlining my code yet the error remains.