$Query1 = "INSERT INTO answers (gid,Key1,key2,key3,key4,key5) VALUES ($gid,'$key1','$key2','$key3','$key4','$key5');"
function Invoke-MySql {
param($Query)
$MySQLAdminUserName = 'root'
$MySQLAdminPassword = <password>
$MySQLDatabase = <username>
$MySQLHost = 'localhost'
$ConnectionString = "server=" + $MySQLHost + ";port=3306;uid=" +
$MySQLAdminUserName + ";pwd=" + $MySQLAdminPassword +
";database=" + $MySQLDatabase
Write-Log "Trying to connect MySql" 0
try {
[void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
$Connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$Connection.ConnectionString = $ConnectionString
$Connection.Open()
$Command = New-Object MySql.Data.MySqlClient.MySqlCommand($Query, $Connection)
$DataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($Command)
$DataSet = New-Object System.Data.DataSet
$RecordCount = $dataAdapter.Fill($dataSet, "data")
$DataSet.Tables[0] | ft -AutoSize
Write-Log "Querying the table with following query: $Query" 0
} catch {
Write-Log "ERROR : Unable to run query : $query `n$Error[0]" 2
} finally {
$Connection.Close()
Write-Log "MySql connection closed" 0
}
}
$Result1 = Invoke-MySql -Query $Query1
OUTPUT:
PS D:\MYSQL> D:\MYSQL\MySql_Insert&Query.ps1 gid key1 key2 key3 key4 key5 --- ---- ---- ---- ---- ---- 31657 c1 c2 c3 c4 c5 31667 b1 b2 b3 b4 b5 112322 aa bb cc dd ee 212322 aa bb cc dd ee 212982 aa bb cc dd ee 215982 aa bb cc dd ee 215987 aa bb cc dd ee 315987 aa bb cc dd ee
So this is the output. I've fetched the results from MySQL, but now I want to filter the results like where I want to fetch only one value eg., key3 of gid=2122322.
$Result1 | Where-Object {$_.gid -eq '2122322'}, or write a new query to do a select with similar criteria.