Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell中MySQL的奇怪行为_Mysql_Powershell - Fatal编程技术网

Powershell中MySQL的奇怪行为

Powershell中MySQL的奇怪行为,mysql,powershell,Mysql,Powershell,我是PowerShell新手,对在PowerShell中使用MySQL有一个特别的问题 我得到了这个函数: Function run-mySQLInsertQuery{ param( $connection, [string[]]$insertQuery ) foreach ($command in $insertQuery){ $MySQLCommand = $connection.CreateCommand()

我是PowerShell新手,对在PowerShell中使用MySQL有一个特别的问题

我得到了这个函数:

Function run-mySQLInsertQuery{
    param(
        $connection,
        [string[]]$insertQuery
    )
    foreach ($command in $insertQuery){
        $MySQLCommand = $connection.CreateCommand()
        $MySQLCommand.CommandText = $command
        $rowsInserted = $MySQLCommand.ExecuteNonQuery()
        if ($rowsInserted) {
            return $rowsInserted
        } else {
            return $false
        }
    }
}
[String[]]$statements = ""

foreach($key in $arrayStatus.Keys){
    $item = $arrayStatus[$key]
    $insertStatus = "INSERT INTO tx_tphbusinessofferings_domain_model_status (status_id, status) VALUES ('$key', '$item')"
    $statements += $insertStatus
}
$Rows = run-mySQLInsertQuery -connection $mySQLconnection -insertQuery $statements
使用此版本的函数,我得到以下错误:

Cause:
"The CommandText property has not been properly initialized."

Errorline:
        $rowsInserted = $MySQLCommand.ExecuteNonQuery()
我搜索了一个解决方案,并将我的函数编辑为以下内容(出于测试目的):

有了这段代码,函数可以毫无问题地执行查询。我现在的问题是,为什么?我看不出有什么区别,因为在
$command
中应该是与
$abcd
中完全相同的查询。还是我做错了什么

编辑: 正如它在评论中所问的那样,以下是我如何调用该函数:

Function run-mySQLInsertQuery{
    param(
        $connection,
        [string[]]$insertQuery
    )
    foreach ($command in $insertQuery){
        $MySQLCommand = $connection.CreateCommand()
        $MySQLCommand.CommandText = $command
        $rowsInserted = $MySQLCommand.ExecuteNonQuery()
        if ($rowsInserted) {
            return $rowsInserted
        } else {
            return $false
        }
    }
}
[String[]]$statements = ""

foreach($key in $arrayStatus.Keys){
    $item = $arrayStatus[$key]
    $insertStatus = "INSERT INTO tx_tphbusinessofferings_domain_model_status (status_id, status) VALUES ('$key', '$item')"
    $statements += $insertStatus
}
$Rows = run-mySQLInsertQuery -connection $mySQLconnection -insertQuery $statements

问题是您正在使用空字符串初始化数组(您正在传递的数组):

[String[]]$statements = ""
然后向其中添加元素。。。因此,传递数组的第一次迭代是一个空字符串,它将不起作用(它将命令文本设置为空,这就是您得到的错误)。它适用于第二个代码,因为您正在获取数组的第二个对象(即insert语句)

将数组初始化为空,它应该可以工作:

[String[]]$statements = @()

除此之外,您的第一个脚本总是在第一次迭代时返回,因此它只工作一次(不是每次通过
insert
)。如果传入多个查询,则不确定要返回什么,但这取决于您的设计决策

因此,在编辑的函数中,您要多次运行第一个
insertQuery
值。这表明问题出在其他insertQuery值中……如何调用函数?你能举一个失败参数的例子吗?@Jcl我已经编辑了我的问题,并演示了如何调用该函数@gvee我不认为这是个问题,因为我在一个数组中循环我想插入的值。我只是更改了值,而不是查询本身,正如你在我所做的编辑中看到的那样。非常感谢!现在它像一个符咒一样工作:-)