[Home]
SQLite

| CodeRaptors | Projects | Tips and Scripts | Guides | Links |

A free open source relational database engine with great performance and lots of features.


Whenever you want to save and process data and would like to use SQL but feel that a full network database daemon (like MySQL) is too much, you should consider SQLite. Even if you only need to save relatively small amounts of simple data you get great performance, automatic synchronization of access and safe atomic writes. If your application needs to handle many concurrent reads/writes or keeps crashing and corrupting data, all the better. The database is just a file so you will get great performance (provided that you are using a sane filesystem and that you don't do much concurrent writing) since no sockets are needed, and the safe writing will guard your data much better than simply writing flat files.

Now you may be thinking "using simple text files are easier" or "I only write scripts, I don't compile software", well SQLite can be of great use anyway. First, using SQL to access saved data is typically a lot simpler than using your own strange home made text parser (and a lot safer). Second, SQLite comes in many shapes and sizes. It can be compiled into a C project or you can use the executable, which is available for download on the homepage, from within a script. There are even native bindings available for PHP, Perl, Python, Ruby, Tcl... There are no excuses, try it!

Some example usage:
 #!/bin/bash
 sqlite3 dbfile "create table stuff (id INTEGER PRIMARY KEY, name TEXT)"
 sqlite3 dbfile "insert into stuff (name) values ('Skruvmejsel')"

 #!/usr/bin/perl
 use DBI;
 my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","","");
 $dbh->do('INSERT INTO stuff (name) VALUES ("Skiftnyckel")'); 

 user@host:~$ sqlite3 dbfile "select * from stuff"
 1|Skruvmejsel
 2|Skiftnyckel
 user@host:~$ sqlite3 dbfile
 SQLite version 3.6.10
 Enter ".help" for instructions
 Enter SQL statements terminated with a ";"
 sqlite> insert into stuff (name) values ('Hammare');
 sqlite> select * from stuff;
 1|Skruvmejsel
 2|Skiftnyckel
 3|Hammare

// myprogram.c

#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"

static int callback(void *not_used,
                    int number_of_columns,
                    char **values,
                    char **column_names)
{
  int i;
  for(i = 0; i < number_of_columns; i++)
  {
    printf("%s = %s\n",
           column_names[i],
           values[i] ? values[i] : "NULL");
  }
  return 0;
}

int main(int argc, char **argv)
{
  sqlite3 *database;
  char* error_message;
  int return_code;

  if(argc != 3)
  {
    fprintf(stderr,
            "Usage: %s DATABASE SQL-STATEMENT\n",
            argv[0]);
    exit(1);
  }

  return_code = sqlite3_open(argv[1], &database);
  if(return_code != SQLITE_OK)
  {
    fprintf(stderr,
            "Can't open database: %s\n",
            sqlite3_errmsg(database));
    sqlite3_close(database);
    exit(2);
  }

  return_code = sqlite3_exec(database,
                             argv[2],
                             callback,
                             0,
                             &error_message);
  if(return_code != SQLITE_OK)
  {
    fprintf(stderr,
            "SQL error: %s\n",
            error_message);
    sqlite3_free(error_message);
    sqlite3_close(database);
    exit(3);
  }

  sqlite3_close(database);
  return 0;
}

 user@host:~$ gcc -o myprogram sqlite3.c myprogram.c
 user@host:~$ ./myprogram dbfile "select * from stuff"
 id = 1
 name = Skruvmejsel
 id = 2
 name = Skiftnyckel
 id = 3
 name = Hammare

Search: