2

I need to be able to do this via command line in one step:

lab-1:/etc/scripts# sqlite3 test.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> .mode csv ;
sqlite> .import /tmp/test.csv users
sqlite> select * from users;
John,Doe,au,0,"",1,5555,91647fs59222,audio
sqlite> .quit

I've tried the following:

 lab-1:/etc/scripts# sqlite3 test.db ".mode csv ; .import /tmp/deleteme.csv users"

and

 lab-1:/etc/scripts# sqlite3 test.db ".mode csv .import /tmp/deleteme.csv users"

I don't get errors but I also don't end up with any data in the users table.

Any tips would be appreciated.

5

SQLite meta commands are not terminated by ; but by newline. Therefore, you will have to provide the commands in some other way so that newlines are inserted in the correct places.

Here's a few examples, of which I would probably use the first one (because it's readable).

  1. Use a here-document:

    sqlite3 testdb <<END_COMMANDS
    .mode csv
    .import /tmp/deleteme.csv users
    END_COMMANDS
    
  2. Format the commands with printf:

    printf '.mode csv\n.import /tmp/deleteme.csv users\n' | sqlite3 test.db
    
  3. Use a here-string with C-escapes (in shells that supports it):

    sqlite3 test.db <<<$'.mode csv\n.import /tmp/deleteme.csv users\n'
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.