October 3, 2022

Windows -- sqlite3

My ultimate goal is to be able to use sqlite3 from ruby, but first I will need to install sqlite3 itself. Among other things this will give me the sqlite3 command line "cli" tool. I go to this link: The version as of October 3, 2022 is 3.39.4. I download two zip files. One is the "bundle of command line tools", which includes the command line shell. The other is the 64 bit DLL. These are small files. Then on windows I do this:
cd C:
mkdir sqlite
cd sqlite
The trick is how to unzip the sqlite-tools-win32-x86-3390400.zip file. The thing to do is to launch the windows file explorer, go to my downloads folder and right click on the zip file in question. This brings up a dialog that includes an "extract all" option. This brings up a dialog that asks where I would like to extract to. I tell it c:\sqlite, but it ends up creating a directory in that folder and extracting into that. Windows has a command "move" that I can use to move the files to where they should be, and then I use rmdir to get rid of the empty directory.

They tell me to also unzip the DLL file into this same directory. So I repeat the process for sqlite-dll-win64-x64-3390400.zip. This gives me sqlite3.dll and sqlite3.def alongside the 3 executables in c:\sqlite

Now I am instructed to add c:\sqlite to my path environment variable. I click the windows start button and type "env" to start a search. It gives me a thing that says "environment variables" in the control panel. You can get to this "directly" by rattling your way along from the control panel to System properties, but this is by far easier.

A button near the button offers "Environment variables", so we click it. I find PATH under "system variables", select it, and click edit. Use "new" to add a new directory to the path.

I see an old entry in the path for c:\ruby193 that I should watch out for.

Indeed, now I can type "sqlite3" and get the sqlite3 shell.

None of the tutorials have said a thing about the .dll file, but we will see about that.

I play with the sqlite3 tool, create a database, user insert to add records to a table I create, use select to display the records and so forth. Note that you can feed an editor generated script to the tool via:

sqlite3 fish.db < myscript
This works just fine without a .quit at the end of the file.

Ruby Sqlite3

Now with sqlite3 in place, I am ready to try the ruby sqlite3 gem. It will need somehow to know where sqlite3 is, but let's just try it and see what happens. I do this:
gem install sqlite3
It finishes in just seconds. I am suspicious, but let's see. (See below, everything works out just fine). A ruby script with the require line for sqlite3 goes without errors. Maybe we are OK and it knows what to do, deeper tests are required.

Just for possible future reference, the following gives a short ruby script that extracts data from an Excel spreadsheet and loads it into a sqlite database.

I used the sqlite3 tool to create a silly database. The question now is whether I can write a ruby script that will find that database and display the records.

I put the following into a file named "silly.sq":

create table person ( id integer, name text );
insert into person values ( 1, "joe" );
insert into person values ( 2, "fred" );
insert into person values ( 3, "sam" );
insert into person values ( 4, "hank" );
Then I give the command:
sqlite3 fish.db < silly.sq
Indeed after doing this I have a non empty file "fish.db". After this I run the following ruby script:
#!/usr/bin/ruby

require 'sqlite3'

db_path = "./fish.db"

db = SQLite3::Database.open( db_path )

db.execute("select * from person") do |result|
      puts "----"
      line = result[0].to_s + ": " + result[1]
      puts line
end

# THE END
This runs just fine, producing the output:
----
1: joe
----
2: fred
----
3: sam
----
4: hank
Note that the initial "shebang" line that is important on a linux system is not needed on windows, but is harmless. I am not sure who or what needs the sqlite3.dll file (or if it is being magically discovered and used). But what is most important is that simply doing "gem install sqlite3" does the job. For all I know it would work without even installing sqlite3 beforehand, but I want the sqlite3 shell, so I may never know or care.

All clear on the western front. Ready to move ahead with ruby and sqlite3.


Have any comments? Questions? Drop me a line!

Adventures in Computing / [email protected]