Accessing Sqlite from a DOS batch file

DOS batch files do not have the most flexible scripting syntax. They are also not the most modern or full featured. One will often use a VBS or Perl script to glue together or bootstrap complex or outdated software.
Even so if you have ever wanted or needed to access a flexible database from a DOS batch file, but though the overhead of installing a full-blown database that understood SQL (so you don’t need to learn some wacky new syntax), now is your chance.

… if you have ever wanted or needed to access a flexible database from a DOS batch file …

I won’t go into all the details of Sqlite but merely show you a start to end set of steps to get you going. I hope once you have seen it in action you will think up new and interesting ways of applying the same techniques in your own projects.

Before I continue I will say that this is not the only database that can perform this sort of thing. When talking about alternatives Berkley DB springs to mind. There are also other alternatives, each with their slant on query languages. For now I will only provide an Sqlite example because it is the new kid on the block and is, up to now, still a little less known than the other (possibly dated) alternatives. One added advantage over all the others is the fact that Sqlite and its code is released into the public domain.

Starting from the very beginning

You will need to download a single executable file, the Sqlite command line shell from the Sqlite download page:
Sqlite runs on numerous operating systems. The version we are looking for will be under the heading “Precompiled Binaries For Windows“.
The filename should something like:

Download and unzip the file and you will find a the executable file: sqlite3.exe
Unzip the file into a convenient directory.
eg. C:\sqlite\

Run the Sqlite shell and create a database file

To run the command line shell (in windows) you can click on Start->Run…
Then enter cmd to open a DOS command shell and press OK.

cd to the directory that sqlite is in or specify the full path, then put the name of the new (to be created database file), I will call mine mydatabase..sqlite.


C:\>cd sqlite
C:\sqlite>sqlite3.exe mydatabase.sqlite

Now we have an empty database.

Create a table and some test data

Enter the following into the Sqlite shell.

  id int,
  name varchar(30),
  phone varchar(30)

INSERT INTO person VALUES (1, 'Jim', '123446223');
INSERT INTO person VALUES (2, 'Tom', '232124303');
INSERT INTO person VALUES (3, 'Bill', '812947283');
INSERT INTO person VALUES (4, 'Alice', '351246233');


Now for the Batch file magic

We will create some batch files that can look up a person’s details given an integer id. You can be more creative.

We will make the .bat files from the DOS command line for this example.

echo @echo  SELECT name from person where id=^%1^;^|  sqlite3.exe mydatabase.sqlite > person.bat
echo @echo  SELECT phone from person where id=^%1^;^|  sqlite3.exe mydatabase.sqlite > phone.bat

What we have done is create two batch files that each take a single parameter (%1) which in this case will need to be the id of the person we are looking for when called. The echo command is used to pass the Sql select statement to the database. person.bat will return the name of the person for the given id. phone.bat will return the corresponding phone number.

To verify this we can take a quick look inside the .bat files

C:\sqlite>type person.bat
echo  SELECT name from person where id=%1;|  sqlite3.exe mydatabase.sqlite
C:\sqlite>type phone.bat
echo  SELECT phone from person where id=%1;|  sqlite3.exe mydatabase.sqlite

Now for the results

C:\sqlite>person.bat 3
C:\sqlite>phone.bat 3

Now we have the details of the person with the id of 3. The SQL can get as complicated as you like.

A single data file (mydatabase.sqlite), one (or more) batch files (person.bat and phone.bat) and a single executable (sqlite3.exe) and you have a fulling working application.


Maybe this would not make a full-blown application but a very powerful, easy to deploy configuration database for a legacy program launcher.

You can get a fancy as you like. It’s as simple as that.


    1. As a follow up, I made a quick attempt at compiling sqlite3 under MS C 5.1 to try and produce a 16-bit binary. There were a few things straight away that prevented it from compiling. SQLITE_API int sqlite3_exec() has a few issues and there are references to long long int sqlite_int64 and unsigned long int that are not supported by the compiler. One other less serious issue is that some of the defined identifiers truncated because they are too long.

  1. Thanks. I only know basic DOS batch and this is so far been the most simple instruction I have seen that made me understand in under a few minutes the full potential for a problem I am solving using sqlite from dos. Thanks A Bunch!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s