3

I want to import a CSV File (comma delimited with double quote) into SQLite using PowerShell script. I tried:

echo ".import export.csv mytable" | sqlite3.exe

I get this error:

export.csv:327: unescaped " character

I get this error for all lines. On SQLite's command line shell same command works:

sqlite > .import export.csv mytable

How can I make this command work using a PowerShell script?

6
  • You may not need the echo (which is really an alias for Write-Output) in PowerShell; try ".import export.csv mytable" | sqlite3.exe. Commented Apr 28, 2017 at 13:55
  • @JeffZeitlin I got same error! Commented Apr 28, 2017 at 13:57
  • 1
    Is the "327" in the error message supposed to represent a line number in the file, or is it an error code that sqlite is reporting? If it is a line number, how many lines does the file have - and how many lines are getting imported when you do it manually? Commented Apr 28, 2017 at 14:02
  • If I remember correctly you need to run it like this: sqlite3 yourdatabase.db < ".import export.csv mytable" Commented Apr 28, 2017 at 15:30
  • 1
    @JamesC. The '<' operator is reserved for future use. Commented Apr 28, 2017 at 15:50

3 Answers 3

3

This works for me in both PowerShell 5.1 and v7.0.

$params = @"
.mode csv
.separator ,
.import export.csv mytable
"@

$params | .\sqlite3.exe mydatabase.db
Sign up to request clarification or add additional context in comments.

Comments

1

The following single command line works in both

  • cmd.exe (version 10.0.x.x via ver) and
  • powershell.exe (version 5.1.x.x via $PSVersionTable)
.\sqlite3.exe my.db ".import file1.csv table1 --csv"

This loads the contents of csv file file1.csv into table table1 within the sqlite database file my.db. The --csv flag will import the file and create the table structure based on the header column names in that file, and will approximately infer the data types.

You can import multiple files this way. i.e.

.\sqlite3.exe my.db ".import file1.csv table1 --csv" ".import file2.csv table2 --csv"

You can chain commands together to immediately open the database for querying by appending ; .\sqlite3.exe my.db.

i.e.

.\sqlite3.exe my.db ".import file1.csv table1 --csv" ".import file2.csv table2 --csv; .\sqlite3.exe my.db"

Comments

0

what about sqlite3.exe ".import export.csv mytable".

You can check the documentation of sqlite3.exe to use it not being in its shell, how to pass parameter to sqlite3.exe.

You can try below line

Invoke-Expression 'sqlite3 yourdatabase.db < ".import export.csv mytable"'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.