Ritik Goyal
Ritik Goyal

Reputation: 7

/usr/bin/sqlite3: argument list too long

I'm trying to insert the contents of a file into an sqlite database, which is about 350k characters long.

VALUE=$(cat file)
sqlite3 database.db "UPDATE table SET value='$VALUE';"

But, I get an "Argument list too long error"

I tried looking it up, but most of the solutions are for find, ls, etc. How do I do this?

Upvotes: -1

Views: 619

Answers (3)

Amin SZ
Amin SZ

Reputation: 379

This one-line code should work:

sqlite3 database.db "UPDATE table SET value=readfile('file');"

Upvotes: 0

Shawn
Shawn

Reputation: 52364

The sqlite3 shell has a readfile() function that returns a blob with the contents of the given file.

$ sqlite3 foo.db
sqlite> CREATE TABLE example(file TEXT, contents BLOB);
sqlite> INSERT INTO example VALUES('foo.txt', readfile('foo.txt'));

or whatever

Upvotes: 2

chepner
chepner

Reputation: 531165

You are on an operating system which has upper limits on how long the command line can be, measured in bytes. (Most OSes have such a limit.) The last argument to sqlite3, because it contains the contents of the file, measures over 350k bytes.

Instead, you should use a COPY command to have sqlite3 read directly from the file. Something like

sqlite3 database.db "COPY table FROM file"

You will probably need to edit the data file slightly to provide the data in a format COPY expects.

Upvotes: 0

Related Questions