3

Im writing a script in powershell which is supposed to connect to a MySQL server, create a new database, and then run query commands against that database. I have a premade .sql file with all of the commands to be executed, however there seems to be a problem with my code where it never actually executes the .sql file. Any help would be appreciated. My code is below.

[void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")

#Creates a connection to the server
$connStr ="server=mySql;Persist Security Info=false;user id=" + "username" + ";pwd=" + "pass" + ";"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)

#Open connection
$conn.Open()

#Drops database if it currently exists
$cmd = New-Object MySql.Data.MySqlClient.MySqlCommand
$cmd.Connection  = $conn
$cmd.CommandText = "DROP DATABASE IF EXISTS " + $dbname
$cmd.ExecuteNonQuery()

#Create the new database
$cmd.CommandText = 'CREATE SCHEMA `' + $dbname + '`'
$cmd.ExecuteNonQuery()

#Load the sql file
$sql = (Get-Content C:\"path to sql file") -join "'r'n"
$cmd.CommandText = $sql

#Close connection
$conn.Close()

1 Answer 1

1

Aren't you missing a $cmd.ExecuteNonQuery() call ? And it looks like you're joining the file lines with the literal "'r'n". You need to use the back tick character instead (e.g "`r`n"). I would use the Out-String cmdlet to convert the file content to one string:

#Load the sql file
$sql = Get-Content C:\"path to sql file" | Out-String
$cmd.CommandText = $sql
$cmd.ExecuteNonQuery()
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your very prompt response. I was under the assumption that I did not need the ExecuteNonQuery call since I am in fact running query's. If I place that call I get the error of no database selected. Sorry I am very new to this so I may be making some fundamental flaws. Also, my sql file contains ~20+ query's is that a problem when converting to one string?
My answer is based on your code and what seems to me to be missing or wrong. Does any of the previous commands worked (dropping or creating the db)? Is the file content (queries) needs to be run one line at a time?
Yes the code that I have above will create and drop the database as expected. The sql file itself contains the commands to Create tables for my database. An example would be: CREATE TABLE IF NOT EXISTS billable_items_checkoffs ( ID int(11) NOT NULL auto_increment, FL int(11) default NULL, UC varchar(50) default NULL etc.
Thank you Shay for your help debugging my code. As it turns out both -join "rn" and Out-String work. The problem I had actually came from my sql script. It contained comments which caused the file to not be read in proper.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.