I have a dumpfilename.sql that I want to import into a swift String for a queries execution. My problem is the utf8 encoding that is not working.
Here is the function I use:
if let filepath = Bundle.main.path(forResource: "dumpfilename", ofType: "sql") {
do {
str = try String(contentsOfFile: filepath, encoding: String.Encoding.ascii) //String.Encoding.utf8
str = String(utf8String:str)!
str = self.removeSubStringIn(mainStr: str, fromStr: "/\\*", toStr: "\\*/", includingFromAndTo: true)!
} catch {
// contents could not be loaded
}
} else {
// example.txt not found!
}
Here is an extract that is not working in the .sql file (bad é and ̩ appearing)
INSERT INTO `Table_CNIType` VALUES (2,0,'Carte d\'identité','CI','','','',1);
INSERT INTO `Table_CNIType` VALUES (2,0,\'Carte d\\\'identité\',\'CI\',\'\',\'\',\'\',1);
I tried to use String(contentsOfFile: filepath, encoding: String.Encoding.utf8)
that is not working. It makes me an empty string.
only String.Encoding.ascii
& String.Encoding.macOSRoman
is working
in terminal, the command file -I gives=> dumpfilename.sql: text/html; charset=utf-8
EDIT 1:
May be it should be text/plain; charset=utf-8
?
Here is the file example
The file is generated with the mysqldump command with the parameters: '--compact', '--skip-set-charset', '--skip-opt', '--default-character-set=utf8'
The swift error message is: Code=261 "The file “dumpfilename.sql” couldn’t be opened using text encoding Unicode (UTF-8)."
EDIT 2:
The error is because there is some AES encrypted data in it like this #õgñÚCódñ^J¥]˚m%r9†U*3j–xR+“páπ§
, so it shows me the é
instead of the é
for example.