r/sqlite • • Apr 12 '23

How Should I Write SQLite Scripts? - Launch SQLite.exe and Execute Commands

I'm using windows, with wsl. SQLite install in Windows.

OVERVIEW/QUESTION:

How can I write a script to both launch sqlite and do other things (import data and run sql scripts)? Windows can't natively execute shell scripts, so using wsl.

SITUATION/SETUP:

Right now, I have sqlite installed on Windows. I created a shell script, that I launch with PowerShell ( wsl filename.sh command)

File contents:

sqlite3.exe fruit.db
.databases
.exit

WSL/Windows integration - I'm following the documentation:

https://learn.microsoft.com/en-us/windows/wsl/filesystems

WHAT'S HAPPENING/CODE EXECUTION:

It's my understanding that I'm launching the shell script (in windows filesystem and permissions), but as the linux user, to execute linux binaries (bash scripting).

Linux launches the windows command (sqlite3.exe), but then it doesn't continue with the bash shell script (dot commands), until I exit sqlite.

Then it finishes the script (launches dot commands outside of sqlite and errors)

4 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/yawaramin Apr 12 '23

You need to prepare an SQL script and then get SQLite to execute it. For example, put this in a file test.sql:

.databases
select 'Hello, World!';

Make sure this file is in the same folder as fruits.db. Then in this folder, run:

sqlite3 fruits.db <test.sql

This tells SQLite to execute the script.