SQLite3 PowerShell input redirection

In Linux or WSL (Windows Subsystem for Linux) the less than or left angle bracket < is used for input redirection.

For example:

sqlite3 ex1.db < ex1.sql

Run the same command in PowerShell, and you will get the following error:

The '<' operator is reserved for future use.
   + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
   + FullyQualifiedErrorId : RedirectionNotSupported'

In the LSQLTHW book Zed has listed the following alternative:

sqlite3 ex1.db -init ex1.sql

and it works!

But if like me you want to be rebellious and use the native redirection of PowerShell.
Then you need to use the pipe operator | like this:

Get-Content ex1.sql | sqlite3 ex1.db

Get-Content loads the file into the PowerShell pipeline and the pipe | command pipes the data to the stdinput of the next command.

If you spend a lot of time using PowerShell, this may be the preferred way of doing it as it is consistent with other native PowerShell commands.
But that is longer to type!!!

Get-Content can be shortened to GC using the built-in alias.
gc ex1.sql | sqlite3 ex1.db

Still too long?

Add your own alias for sqlite3

New-Alias -Name SQL3 -Value "sqlite3" # remember to save this to you PowerShell profile.

Now in PowerShell we get this:

gc ex1.sql | sql3 ex1.db

Short enough to type over and over again and uses the native input redirection.

NOTE: It is assumed in the above commands that sqlite3.exe is in your windows path.

1 Like

Nice, good work around. I have no idea why PowerShell has ever redirection but < (and also totally fails at Unicode and utf-8).

A free service run by Zed A. Shaw for learncodethehardway.org.