ccc31807
ccc31807

Reputation: 773

Exit to shell after running a SQLite script in PowerShell

For years (at least since 2004) I've developed databases using SQLite and running the scripts under the command prompt, like this:

sqlite3 mytest.db < mytest.sql > mytest.txt

This runs the script contained in mytest.sql and outputs to mytest.txt, and then returns back to the OS prompt. I can use a quick edit, test, and review cycle.

However, when I try the same with PowerShell, like this:

.\sqlite3 mytest.db -init mytest.sql > mytest.txt

I get stuck and have to Ctrl + C to escape back to the prompt. Otherwise, it's just the same as in the command prompt.

I've tried the following in my SQL script:

  1. .exit [SQLite dot command]
  2. .system Exit
  3. .system Exit-PSSession
  4. .system Exit-PSHostProcess
  5. piping the command (with | Exit-PSHostProcess)
  6. using semicolon searated statements (with ; Exit-PSHostProcess)

But nothing seems to work. Doing Ctrl + C isn't a big deal, but it's aggravating to have to do it. Is there a way to return to the PowerShell prompt after running the SQLite script?

Upvotes: 2

Views: 606

Answers (2)

PerseP
PerseP

Reputation: 1197

The input redirection < is reserved in powershell so it won't work. You have to do something like this:

gc .\mytest.sql | sqlite3.exe mytest.db > mytest.txt

After running the commands in \mytest.sql it will exit automatically

Upvotes: 0

Ozywart
Ozywart

Reputation: 11

I'm replying to a very old post, but well, I have the same problem and I just found the solution.

Maybe this will be useful for someone else :

sqlite3 .\mytest.db -init .\mytest.sql -batch .exit

Upvotes: 1

Related Questions