如何使用Powershell和MySQL打印受影响行的输出?

时间:2014-10-28 16:11:50

标签: mysql powershell powershell-v2.0 powershell-ise

我对如何一个接一个地执行两个查询有一点问题。

在我的Powershell脚本中,我在循环中添加记录(完全正常)。在LOOP之外,我执行一个查询,根据TIMESTAMP(正在工作)删除“x”个记录,但在执行之后,我想运行一个查询来显示受影响的ROWS的数量并显示它。以下是我的代码。

提前致谢!

### ============================================================= 
       ### Deleting every record that is 30 minutes old
       ### =============================================================  

        $command = $connection.CreateCommand();
        $command.Connection  = $connection;
        $command.CommandText = "DELETE FROM workstation_userlogged
                                WHERE lastupdate < (NOW() - INTERVAL 30 MINUTE)";


        $records_deleted = "SELECT row_count()"; #count number of rows affected by the delete query

        $command.CommandText = "$records_deleted";

         TRY{
            $command.ExecuteNonQuery() | out-null;
            Write-Host "Successfully deleted $records_deleted records from the Database" -ForegroundColor Green;
        }
        CATCH{
            Write-Host "Caught the exception" -ForegroundColor Red;
            Write-Host "$_" -ForegroundColor Red;
        }

1 个答案:

答案 0 :(得分:1)

如果您想要为您返回结果集,则无法使用ExecuteNonQuery(您执行此操作...这就是您运行SELECT row_count()的原因)。请改用ExecuteScalar方法。