sqlite3 import with quotes

泄露秘密 提交于 2019-12-05 04:40:23
Doug Currie

The web page you reference is old (note the cvstrac portion of the URL, which is the giveaway; sqlite uses fossil now, not cvs). The newer version of that web page is here.

Since SQLite is public domain software, one solution to your problem is to fix sqlite's shell.c to handle your file format correctly. The problem is around line 1861 that does

if( c=='"' ) inQuote = !inQuote;

If you don't want quote delimiting, just comment out this line. The purpose of the line is so you can embed delimiters in your columns by quoting the column.

Another approach is to use a Database Manager that supports SQLite; there are many of them, and most claim to support file import/export.

Sqlite3's .import tool behaves as if its input is in comma-separated-values format, even if .separator is not a comma. You can escape quotes (") by doubling them ("") and quoting the entire field they occur in.

I got a clean .import of your test data after running it through the following filter:

sed 's/"/""/g;s/[^\t]*/"&"/g' file.txt >quoted.txt

The sqlite3 command-line tool isn't very flexible in what import formats it supports.

You could

  • change the import file to add double quotes around and escape double quotes in fields; or
  • convert the import file into a series of SQL statements:

    INSERT INTO MyTable VALUES(
        1193782372,
        'Lips Like Sugar (12" Mix)',
        'Echo & the Bunnymen 80''s/12": The Extended Collection',
        'a76d9b04-51d9-4672-801f-356ab36dbae7',
        'ccd4879c-5e88-4385-b131-bf65296bf245',
        '1abb270a-e791-407f-a989-ff3ad6f8401c');
    

    or

  • write your own import tool.
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!