0

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.

Community
  • 1
  • 1
ΩlostA
  • 2,501
  • 5
  • 27
  • 63
  • 2
    What is the String encoding of your sql file? – Leo Dabus Feb 06 '19 at 14:53
  • 3
    You need to use the actual encoding used by the file. – rmaddy Feb 06 '19 at 14:55
  • @LeoDabus the string encoding is text/plain; charset=utf-8 (using the file file -I command on the mac) – ΩlostA Feb 06 '19 at 15:01
  • Are you sure? It wouldn't return nil. try utf16 – Leo Dabus Feb 06 '19 at 15:02
  • @LeoDabus it makes me japanese character "⼪ℴ〱〱⁓䕔⁀獡癥摟捳彣汩敮琠††㴠䁀捨慲慣瑥牟獥瑟捬楥湴‪⼻ਯ⨡㐰㄰ㄠ卅吠捨慲慣瑥牟獥瑟捬..." – ΩlostA Feb 06 '19 at 15:04
  • `String(utf8String:)` where is it declared? Whats the purpose of it? Seems to me that your sql file it is probably `.ascii` encoded. Note that all swift strings are utf8 encoded by default. `String(contentsOfFile: filepath, encoding: .ascii)` should be enough. – Leo Dabus Feb 06 '19 at 15:07
  • If you need to encode your swift string as .utf8 is simple just pass your string UTF8View to a data initialiser `Data(str.utf8)` – Leo Dabus Feb 06 '19 at 15:13
  • @LeoDabus When I open it with *TextWrangler*, it is not the good characters, but if I open it with *Sublime Text* the characters are good – ΩlostA Feb 06 '19 at 15:25
  • Have you tried with a different sql file? Seems to me that your file is not properly encoded. – Leo Dabus Feb 06 '19 at 15:26
  • Have you tried `isoLatin1` string encoding? `let str = String(contentsOfFile: filepath, encoding: .isoLatin1)` – Leo Dabus Feb 06 '19 at 15:30
  • @LeoDabus It does the same. I made a mistake, the file -I is giving *text/html; charset=utf-8* and not *text/plain; charset=utf-8*. Is there something on that side? – ΩlostA Feb 06 '19 at 15:46
  • If you need to load an html file you need to use NSAttributedString initializer and pass the document type .html https://stackoverflow.com/questions/28124119/convert-html-to-plain-text-in-swift/28132610#28132610 – Leo Dabus Feb 06 '19 at 15:47
  • @LeoDabus I checked the error, it is because in the file, I have some AES encrypted fields with data like this: *#õgñÚCódñ^J¥]˚m%r9†U*3j–xR+“páπ§* . If I remove them, it works. I have to find a solution to that. Thanks for your help – ΩlostA Feb 06 '19 at 17:28

0 Answers0