對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2016/2/15 2:44:30
PowerShell MySQL querie syntax
I want to insert Data in my MySQL Database by PowerShell. My Code is working, but I have a little problem with the varables in the insert string.
How can I use a variable in the insert string?
mysql powershell powershell-v2.0 mysql-connector
Answer
Try using double quotes for sql string, single quotes around individual variables and ensuring your concatenated variables are quoted:
原文出處:
PowerShell MySQL querie syntax - Stack Overflow
I want to insert Data in my MySQL Database by PowerShell. My Code is working, but I have a little problem with the varables in the insert string.
How can I use a variable in the insert string?
$cn = New-Object -TypeName MySql.Data.MySqlClient.MySqlConnection
$cn.ConnectionString = "SERVER=localhost;DATABASE=test;UID=root;PWD=pwd"
$cn.Open()
$cm = New-Object -TypeName MySql.Data.MySqlClient.MySqlCommand
#The problem, not working
$n = 7
$sql = 'INSERT INTO db1ea4test1.besitzer (besID, Vorname, Name) VALUES ( '$n' , "Testvor" + '$n' , "Testnach" + '$n')'
# Working
#$sql = 'INSERT INTO db1ea4test1.besitzer (besID, Vorname, Name) VALUES ("6", "Testvor", "Testnach")'
$cm.Connection = $cn
$cm.CommandText = $sql
$dr = $cm.ExecuteNonQuery()
mysql powershell powershell-v2.0 mysql-connector
Answer
Try using double quotes for sql string, single quotes around individual variables and ensuring your concatenated variables are quoted:
$sql = "INSERT INTO db1ea4test1.besitzer (besID, Vorname, Name) VALUES ( '$n' , 'Testvor$($n)' , 'Testnach$($n)')"
原文出處:
PowerShell MySQL querie syntax - Stack Overflow