我在Powershell中使用以下函数尝试将我的SQL服务器上的文件加载到现有表中:
$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
最后,我得到的错误:
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;
我认为通过尝试研究它,我已经尽可能地打败了它。
当我在我的服务器上运行SQL文本(减去转义字符)时,它可以正常运行。甚至使用用户ID和密码直接登录到SQL服务器,我通过Powershell脚本发送。
.CommandText向我显示正在发送的文本LOOKS适当(下面):
{{1}}
任何人都知道我在这里做错了什么?