Command Prompt Queries in Batch File
(1) By Tomel51 on 2022-04-23 15:34:15 [link] [source]
Using Windows 11. Trying to create some query batch files to make it easier for users to extract various data tables by simply clicking a link for the desired output. My plan is to develop a hyperlink index in, perhaps, Excel or Word, with links to maybe 20 different batch file queries. To illustrate where I am at this point, here's a sample batch file: sqlite "My Database.db" .mode csv .output FinalReport.csv SELECT * FROM theTable This works when I copy it and paste it into the command prompt. However, several problems I haven't been able to solve. I have to open the command prompt manually, then I either have to change the directory to the database location or include the entire path along with the db name when I execute sqlite. Then I can run the above query, but have to hit return to get it to finish and create the csv file. I tried adding .quit to the end so the user would not have to hit the return key. Still had to hit return to get results. I've tried including the start cmd to open the command prompt and cd command to change the directory at the beginning of the batch file. No output was generated. Any suggestions on how to change the batch file so it can simply be double-clicked by a user and the final csv output table appears. Would be nice to simply also close the command line window as part of the process. The less the user has to do -- or even see -- the better.
(2) By RandomCoder on 2022-04-23 15:59:23 in reply to 1 [link] [source]
A batch file is, roughly, a list of commands for the command interpreter. The commands for SQLite are commands for SQLite. In other words, you'll need some way to send those commands to the SQLite shell. One possible answer is to create a little text file with the list of commands, and redirect it into the shell. You could either make that text file as a separate file, or have the batch file create it so the script is somewhat self contained:
@echo off
rem Change to the directory of this batch file, so "double clicking" on this file
rem will operate on files in the same folder as this script.
cd /d "%~dp0"
rem Create a temp file to execute commands
>temp_sql_commands.txt echo .mode csv
>>temp_sql_commands.txt echo .output FinalReport.csv
>>temp_sql_commands.txt echo SELECT * FROM theTable
rem Call SQLite with the command script
sqlite3 "My Database.db" < temp_sql_commands.txt
rem Remove the helper text file
del temp_sql_commands.txt
echo All done, created FinalReport.csv
(3.5) By Aask (AAsk1902) on 2022-04-23 17:34:03 edited from 3.4 in reply to 1 [link] [source]
This works when I copy it and paste it into the command prompt.
When pasting into the Command Prompt,your first line starts & focuses on the SQLite CLI; the second and subsequent lines go to the CLI where they make sense.
When executing the same set of lines/statements from a batch file,all the lines are echoed to the Command Prompt NOT the SQLite CLI. That's why it does not work.
You are almost there!
First, your batch file (it should have extension BAT or CMD such that double click on it from within File Explorer will execute it).
"D:\SQLite32\sqlite3.exe" < "e:\temp\aa2.txt"
That is, specify the fully qualified name of SQLIte3.EXE, in quotes (permits the path to have embedded spaces) followed by the fully qualified name of another file, e:\temp\aa2.txt in my example.
The left chevron, <, redirects input from e:\temp\aa2.txt to SQLite3.EXE
Second your SQLite lines of code in e:\temp\aa2.txt
.open 'D:/SQLite32/DB/CHINOOK.DB'
.mode csv
.once e:/temp/A_sqlite.csv
select * from employees;
The .once command redirects output to the file e:/temp/A_sqlite.csv: this file will be overwritten if it exists without warning.
Adapt the contents of the files (the two blocks of code) to your circumstances and it should work else raise your queries here.
Also, consider the option to send the output of your SQL statement directly to Excel (thereby giving your users even less to do!).
.once -x
select * from employees;
The above will start a new Excel instance and send the output directly to that instance (which should be installed on the computer). In Excel you will need to AutoFit Column Width.
but have to hit return to get it to finish
Usually, you would simply insert a blank line in your file to overcome this; in this case, you need to add semi-colon at the end of your SQL statement.
PS: You can (should) use / rather than \ for path separators since / is safe(r) for SQLite and fully accepted in Windows.
(4) By Tomel51 on 2022-04-23 18:24:19 in reply to 3.5 [link] [source]
Thanks to both of you who responded to my question. It works! I was able to create a hyperlink from Excel that calls the batch file which either creates (Option 1) or calls an existing (Option 2) text file which creates and saves the output file. Can't make it much easier for the casual user.
(5) By Aask (AAsk1902) on 2022-04-23 19:50:52 in reply to 4 [link] [source]
Can't make it much easier for the casual user.
You can.
As things stand, the extract or result of your SQL statement is static. In practice that means the following:
- the user has a historical snapshot of the underlying data (which has some merits and can be useful for forensic analysis).
- The user has to run the script afresh to get the underlying data (which might have changed).
As you appear to be driving everything from Excel, you might consider refreshable workbooks.
For this you'll need:
a. To set up, pursue Data | Get Data | From Other Sources | From ODBC within Excel.
b. CARE! These links that follow will download the drivers For ODBC, you will need either the 32-bit driver or the 64-bit driver depending on whether you have the 32- or 64- bit version of Excel.
Then, in Excel, the user clicks Data | Refresh All to refresh the data by re-reading the underlying data from SQLite.
Advantages:
- Your SQL is not exposed (in text files).
- The user is empowered - they can choose when to refresh the data.
- Subject to available memory, all the data you present to the user can be contained in one workbook and in different worksheets. This is easier to maintain than a series of individual text files.
- You might also have charts based on the data drawn from Excel; these will refresh automatically as the data changes.
- Any cosmetic rendering such as number formatting or font changes also tend to stick.
This works well for internal users.
Disadvantages:
The ODBC drivers mentioned above are statically linked to a historical version of SQLite and you have no control over the timing of their update. That means you will forgo the opportunity to use the latest enhancements in SQLite.
PS: If you download and install the ODBC drivers, you will NOT need to download any SQLite pre-compiled binaries.
(6) By Tomel51 on 2022-04-23 21:57:41 in reply to 5 [link] [source]
Thanks for the additional suggestion. I'm still not entirely sure what my final "product" will look like. Not all users have Excel. I've considered outputting to a CSS-formatted HTML table. I could also create my index and hyperlinks in HTML as well since everybody has a browser. In the short term, I'll be filling informational requests either using my "canned" queries or creating custom queries for special requests. I ultimately want to set up something where I'm not the only person that's relied on for data needs. I've even considered using python to create a user-friendly interface. However, I'm anything but a Python expert and going that direction looks like it may require more learning and time than I'm willing to commit to the project. One other thing I have to work out. I have a couple of queries that require variable input. Like find all transactions between two dates or all individuals named Smith. I know how to write these queries, but not how to make them easily accessible to a normal user. Thanks again for the ideas. I'm accumulating any suggestions and things I find on the net and, hopefully, will come up with something that others can use once I back out of the data supply business.
(7) By Aask (AAsk1902) on 2022-04-23 22:44:16 in reply to 6 [link] [source]
I've considered outputting to a CSS-formatted HTML table.
Have a look at the options for .mode; it includes html.
like find all transactions between two dates or all individuals named Smith.
Easily user-accomplished in Excel either with data filter or with pivot tables.
(8) By Donald Griggs (dfgriggs) on 2022-04-25 18:15:28 in reply to 5 [link] [source]
Regarding: Disadvantages...The ODBC drivers...are statically linked to a historical version of SQLite. ... You will forgo the opportunity to use the latest enhancements in SQLite.
Do note that Mr. Werner released versions which use a DLL external to the ODBC driver.
Though marked "experimental" I recall using one without issue -- though perhaps your mileage will vary.
From page: http://www.ch-werner.de/sqliteodbc/
sqliteodbc_dl.exe (EXPERIMENTAL: variant which needs System.Data.SQLite.dll or sqlite3.dll in the Windows system folder)
sqliteodbc_w64_dl.exe (EXPERIMENTAL: variant which needs System.Data.SQLite.dll or sqlite3.dll in the Windows system folder)
(9) By Aask (AAsk1902) on 2022-04-26 08:36:24 in reply to 8 [source]
though perhaps your mileage will vary.
System.Data.SQLite.dll is also statically liked to a previous version of SQLite.
Generally, I am loathe to altering any operating system folder.
However, I tried the 32-bit Experimental ODBC driver with SQLite2.DLL (version 3.38.2) and it did not work for me. The non-experimental version is still linked to version 3.32 and there is no way to simply replace this with v3.38.2.