E
E
Evgeny Kotov2019-02-05 18:54:55
MySQL
Evgeny Kotov, 2019-02-05 18:54:55

How correctly to transfer CommandText?

Good day. I can't figure out the syntax of the database import request.
The MySql database is installed locally. If I do all the actions with my hands, everything works.
The problem is in quotes, Powershell swears that not all quotes and apostrophes are closed.
MySQL expression

LOAD DATA LOCAL INFILE '"$Путь к файлу"' INTO TABLE `bga` FIELDS ENCLOSED BY '"' TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES;

Program code:
[void][system.reflection.Assembly]::LoadWithPartialName("MySql.Data")
$dir = 'C:\Temp\1'
$dir2 = 'C:\Temp\2'
$dbusername = 'root' 
$dbpassword = '123456' 
$dbname = 'curamed' 
$MySQLHost = 'localhost'


$connStr ="server=localhost;database=" + $dbname + ";Persist Security Info=false;user id=" + $dbusername + ";pwd=" + $dbpassword + ";"
$conn = New-Object MySql.Data.MySqlClient.MySqlConnection($connStr)
$conn.Open()


foreach ($i in ls -name $dir\*.csv)
{
  Write-Host "$i"
  $file_content = Get-Content "$dir\$i";
  [System.IO.File]::WriteAllLines("$dir2\$i", $file_content);
  Remove-Item "$dir\$i"
  conn.CommandText = "LOAD DATA LOCAL INFILE '"$dir2\$i"' INTO TABLE `bga` FIELDS ENCLOSED BY '"' TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES;"
  $conn.ExecuteNonQuery()
}

$conn.Close()

For other elegant solutions to my problem, I will be grateful.
Knowledge of Powershell is superficial...
The error code is true in German.
Die Zeichenfolge hat kein Abschlusszeichen: '.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
azarij, 2019-02-05
@azarij

and so?
conn.CommandText = "LOAD DATA LOCAL INFILE '$dir2\$i' INTO TABLE `bga` FIELDS ENCLOSED BY '`"' TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES;"
yes, do you need back quotes around bga?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question