“unescaped ” character" when importing text data into SQLite

后端 未结 2 1574
一个人的身影
一个人的身影 2021-01-15 17:58

I\'m trying to import a semicolon-separated text file where each line ends in CRLF. The first line contains the fields, and the data start at line 2:

\"Field         


        
2条回答
  •  庸人自扰
    2021-01-15 18:25

    For sqlite, when the data to be imported may contain the double-quote character ("), do not use csv mode. The code that reads each CSV field csv_read_one_field looks for it and when it finds it, ensures that it is terminated or expects it to be quoted.

    Changing the column separator to ';' won't help because that code will still be executed.

    On the other hand, the code that reads each field in ascii mode ascii_read_one_field only uses the column and row separators to determine the field contents.

    So, Use ascii mode and set the column and row separators to semi-colon and end of line like this:

    *nix:

    sqlite> .mode ascii
    sqlite> .separator ";" "\n"
    sqlite> .import input.csv MyTable
    

    windows:

    sqlite> .mode ascii
    sqlite> .separator ";" "\r\n"
    sqlite> .import input.csv MyTable
    

    However, that will not strip out the double-quotes surrounding your data; they are considered part of your data.

提交回复
热议问题