Hi new to this. Can someone tell me the easiest way of connecting to MySQL database, running a query and outputting to CSV?
Thanks
Technology Tips and News
Hi new to this. Can someone tell me the easiest way of connecting to MySQL database, running a query and outputting to CSV?
Thanks
Hi,
This should help with connecting to the database and running a query. As for exporting to CSV, all you need to do is build your object and then pipe it to Export-Csv.
http://www.thomasmaurer.ch/2011/04/powershell-run-mysql-querys-with-powershel
Exactly how do you do it?
Example from your script would be helpful!
/Alexander
Thanks, I had fount that but when I do export-csv I get Field Count
I'm not sure what the object returned by the script looks like. You may need to use Select-Object to get certain properties or create your own object specifically for output.
Do what? The link will show you how to connect to MySQL and run a query.Exactly how do you do it?
Example from your script would be helpful!
/Alexander
Example of how it can be done using the example connection string to the database from http://www.thomasmaurer.ch/2011/04/powershell-run-mysql-querys-with-powershell/ :
run-MySQLQuery -ConnectionString "Server=localhost;Uid=root;Pwd=p@ssword;database=project;" -Query "SELECT * FROM firsttest" | Select-Object PropertyThatYouWant | Export-CSV "C:\test\test.csv"
/Alexander
Thanks, I had fount that but when I do export-csv I get Field Count
I'm not sure what the object returned by the script looks like. You may need to use Select-Object to get certain properties or create your own object specifically for output.
Exactly how do you do it?
Example from your script would be helpful!
/Alexander
Do what? The link will show you how to connect to MySQL and run a q
Ah, gotcha. Cheers.Sorry, I was a bit unclear.
What I wanted was DaveHook script of how he tried to export the data to a csv.
Thanks for the help to all of you. I found this which fits my simple mind and works
$constring="server=localhost;uid=XXXX;pwd=XXXX;database=XXXXX;Pooling=False"
$mysql = New-Object MySql.Data.MySqlClient.MySqlConnection($constring)
$mysql.Open()
$sqlquery1 = "select * from wp_users"
$req = New-Object Mysql.Data.MysqlClient.MySqlCommand($sqlquery1,$mysql)
$dataAdapter = New-Object MySql.Data.MySqlClient.MySqlDataAdapter($req)
$dataSet = New-Object System.Data.DataSet
$dataAdapter.Fill($dataSet, "Query1") | Out-Null
$dataSet.Tables["Query1"] | Export-Csv -path "C:\colin\result.csv" -NoTypeInformation