powershell运行load data local infile时出错

5n0oy7gb  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(391)

我正在使用powershell中的以下函数尝试将sql server上的文件加载到现有表中:

Function MySQL-nonQuery($server, $user, $pass, $database, $sql){
    try{ #set up My SQL connection
        [void][System.Reflection.Assembly]::LoadWithPartialName("MySql.Data")
        $connectionString = "server=" + $server + ";port=3306;uid=" + $user + ";pwd=" + $pass + ";database="+$database + ";AllowUserVariables=True"
        $connection = New-Object MySql.Data.MySqlClient.MySqlConnection
        $connection.ConnectionString = $ConnectionString
        $connection.Open()
    }
    catch{
        $Error[0]   
    }
    #set up command
    $updateCOD = New-Object MySql.Data.MySqlClient.MySqlCommand($sql, $connection)
    $updateCOD.CommandText
    $updateCOD.ExecuteNonQuery()
    $connection.Close()

}

这是我的命令执行:

$sql = "LOAD DATA LOCAL INFILE '/var/lib/phpMyAdmin/upload/MyCSVfile.csv' INTO TABLE MySQLTable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '""' LINES TERMINATED BY '\n' IGNORE 1 LINES (@col1,@col2,@col3,@col4,@col5,@col6,@col7,@col8) SET ``field1``= @col2, ``field2`` = @col5, ``field3`` = @col3, ``field4`` = @col4, ``field5`` = @col1, ``field6`` = @col7, ``field7`` = @col6, ``field8`` = @col8;"
MySQL-nonQuery "server" "user" "password!" "database" $sql

最后,我得到的错误是:

Exception calling "ExecuteNonQuery" with "0" argument(s): "Fatal error encountered during command execution."
At C:\FILEPATH\DBupdate.ps1:78 char:5
+     $updateCOD.ExecuteNonQuery()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : MySqlException

我想我已经尽我所能去研究它了。
当我在服务器上运行sql文本(减去转义字符)时,它可以正常工作。甚至使用我通过powershell脚本发送的用户名和密码直接登录到sql服务器。
.commandtext向我显示发送的文本看起来很合适(如下所示):

LOAD DATA LOCAL INFILE '/var/lib/phpMyAdmin/upload/MyCSVfile.csv' INTO TABLE MySQLTable FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' IGNORE 1 LINES (@col1,@col2,@col3,@col4,@col5,@col6,@col7,@col8) SET `device`= @col2, `nsn` = @col5, `hostname` = @col3, `errorsince` = @col4, `screenid` = @col1, `status` = @col7, `opstate` = @col6, `statusmsg` = @col8;

有人知道我做错了什么吗?

368yc8dk

368yc8dk1#

答案是,这根本不是我的代码,而是我正在导入的数据。我的csv中的第一行有一个导致错误的空值。
当我看到这篇文章时,我注意到有人发布了一些关于heidisql的帖子,说它提供了sql命令,可以稍后使用。它非常有帮助地显示了挑出问题的错误。
我试过mysql workbench,但它没有像heidisql那样提供任何错误反馈。

相关问题