SQLite - Can we pass arguments to a .sql file from batch file?

Viewed 1766

I would like to pass an argument from a batch file to SQL file(for SQLite). I had no luck finding this method online, is it possible to do? If not, is there any alternative way?

For example, here is what I wanted to do:

In batch file I wanted to pass %arg1% to sqlTest.sql:

@echo off
set arg1=%1
echo .quit | sqlite3.exe -init sqlTest.sql %arg1%"

In sqlTest.sql file:

.open "db/code_status.db"
SELECT FileName, FilePath, Author
FROM CodeStatus
LEFT JOIN CodeFile ON CodeStatus.ChangeId = CodeFile.ChangeId
WHERE WorkItemId = 2197
ORDER BY ModifiedDate ASC;

For WHERE WorkItemId = 2197, I would like to replace 2197 to the argument value from batch.

Any solution?, thanks!

1 Answers

You might try using the param[eter] dot-command of sqlite command-line shell (section15).

In this particular example, modify the sqlTest.sql:

  • remove the .open line
  • change WHERE WorkItemId = 2197 to WHERE WorkItemId = :id

Then you should get the desired result with something like this batch script:

@echo off
sqlite3.exe "db/code_status.db" ".param set :id %1" ".read sqlTest.sql"
Related