Share your repls and programming experiences

← Back to all posts
C++ Wrapper for SQLite Database
Highwayman (1358)

This is probably the biggest accomplishment for me right here lol.
Let me start from the beginning...

scroll down to tldr for shorter thingy..

there was a point when my only goal at all in terms of databases was to just figure out sql, it was one of my (short term) obsessions that cause many an impulse read. This evolved into me trying to figure out how to connect an sql database to a php server. Now all of this eventually crumbled to pieces because I could not find a single resource that would just tell me how to configure the php server using just the command line. Obviously this was quite frustrating. By then I had entirely given up on just learning sql or connecting so then I tried making my own dB in C++ with memory mapping and all that nice stuff.

Complete. DISASTER.

I was already having a hard enough time figuring though mmaping stuff, serializing data and figuring through if I should make a class or just take the procedural path (I did both. Hated both. Dropped both.) so that didn’t last long either.

Then one day out of the blew, I remembered that the SQLite interpreter becomes a repl if you don’t pass it a file.

HOLLLLLLYYYYYY
My brain exploded inwards. No implosion. Explosion. Yes. Implosion is too weak for what was happening to me right then.

Anyways.

I wrote down 90% of the source code in a day and was up and running happily with some strange read errors before I could blink an eye. Really exciting! :D

This is what I was thinking.

Shells can do this cool thing called "piping", where basically you take one process’s output and use it as the input for a second program, which is really handy for a lot of things and (nerdy me) I had of course already looked into making my own shell, so I knew how to do that in *nix. Another consequence of learning to make a shell had helped me learn about making separate processes in C/C++, which you then replace with a new process to run a program! Awesome!! Anyways I decided I’d basically spawn a SQLite repl and pipe commands to it and pipe the SQLite repl’s output back to my program.

tl;dr

normal program
-> fork()
normal program1,
normal program2,
->pipe() 1 to 2
commands go through pipe and output comes back through different pipe. If your a little confused but know the function calls already, just read my code it’s only like maybe 100 lines.

Anyways. To use it you just do

#include "SQLiteDb.hpp"


int main() {
  SQLiteDb db("name of database file"); // make a database the uses the file.
  std::string command_result  =  db.execStatement("SQLite commands here.");
  db.disconnect(); // disconnects from database and destroys repl process.
}

Anyways hope someone likes it.

Commentshotnewtop
DynamicSquid (3622)

Didn't understand anything, I'm not an SQL guy, but good job!

Highwayman (1358)

@DynamicSquid ope :/ what parts did you not understand? I can clarify.

DynamicSquid (3622)

@Highwayman Everything. I don't know a thing about databases or Shells or using outputs for inputs. Don't worry though, I have never learned this stuff before, so it's not your explanation, it's just me

Highwayman (1358)

@DynamicSquid oh ok. That’s easy enough to explain though. A shell is an entirely textual interface for an OS, for example bash is a shell. When using a program you generally send your messages to the standard output (like cout in c++), and get input from the standard input (like cin in c++). Shells generally have some form of syntax that allows you to use a certain program for file as the standard input or output. For example I can get the program "python" to use the file "replstuff.py" as it’s standard input but doing
python < replstuff.py in bash, because the < op replaces the standard input of a program with the file you hand it, in this case replstuff. Any better?

DynamicSquid (3622)

@Highwayman Oh... okay. So an operating system needs some sort of input or output, and you can use a shell to do that?

Highwayman (1358)

@DynamicSquid ye. Just like you can use a GUI.

DynamicSquid (3622)

@Highwayman Okay. So my computer has (or is) an OS, so I can use Bash to execute commands to it? And the commands could be written in Python?

Highwayman (1358)

@DynamicSquid So yes your computer has and os. And you can use Bash to execute commands to it and if you had the executable "python", then yes they could be written in python.