I have a CSV file UTF-8 format with Spanish diacritics, if I load it from Access and select UTF-8 all is well. I want to automate my work so I made a Powershell script. Because the table is already created I can't load data with SELECT *
but INSERT INTO
. With SELECT *
I can specify the UTF-8 Charset like [text;HDR=Yes;CharacterSet=65001;]
. How can I do that with "INSERT INTO"?
My code so far (working but utf-8 characters are gibberish):
$PSDefaultParameterValues['*:Encoding'] = 'utf8'
$connectstring = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\Nobody\Desktop\Mexico-test.accdb"
$conn = New-Object System.Data.OleDb.OleDbConnection($connectstring)
$conn.Open()
$Voters = Import-Csv -Delimiter "," -Path "C:\Users\Nobody\Desktop\mexico.csv"
foreach ($Voter in $Voters)
{
$curp = $Voter.curp
$age = $Voter.age
$forename = $Voter.forename
$middlename = $Voter.middlename
$surname = $Voter.surname
$fatherSurname = $Voter.fatherSurname
$motherSurname = $Voter.motherSurname
$cmd = $conn.CreateCommand()
$cmd.CommandText="INSERT INTO voters(curp,age,forename,middlename,surname,fatherSurname,motherSurname) VALUES('$curp','$age','$forename','$middlename','$surname','$fatherSurname','$motherSurname')"
$cmd.ExecuteNonQuery()
}
$conn.Close()