[Vala] Another Sqlite example

I ported an example from the Sqlite website into Vala and was wondering if anyone would like to put it on the 
This one is from: http://www.sqlite.org/cvstrac/wiki?p=SimpleCode

I was looking into the Sqlite bindings since I needed to output the result of a select statement to a 
variable and use it as a return value.
Using a callback seemed to make this difficult so in this example it steps though the result of the statment 
inside the method itself.

The code might look a bit messy and one can question whether eight levels of indentation is really necessary 
for a simple Sqlite program.
But this is the way the original C code was structured so I tried to transcribe it as accurately as possible.
using GLib;
using Sqlite;

public class SqliteSample : GLib.Object {
        public static int main (string[] args) {
                Database db;
                Statement stmt;
                int rc = 0;
                int col, cols;

                if ( args.length != 3 ) {
                        stderr.printf ("Usage: %s DATABASE SQL-STATEMENT\n", args[0]);
                else {
                        rc = Database.open (args[1], out db);
                        if ( rc == 1)
                                stderr.printf("Can't open database: %s\n", db.errmsg ());
                                rc = db.prepare_v2(args[2], -1, out stmt, null);
                                if ( rc == 1 ) {
                                        stderr.printf ("SQL error: %d, %s\n", rc, db.errmsg ());
                                else {                        
                                        cols = stmt.column_count();
                                        do {
                                                rc = stmt.step();
                                                switch ( rc ) {
                                                case Sqlite.DONE:
                                                case Sqlite.ROW:
                                                        for ( col = 0; col < cols; col++ ) {
                                                                string txt = stmt.column_text(col);
                                                                stdout.printf("%s = %s\n", stmt.column_name ( 
col ), txt);
                                                        stderr.printf("Error: %d, %s\n", rc, db.errmsg ());
                                        } while ( rc == Sqlite.ROW );
                return rc != Sqlite.DONE ? 0 : 1;

If it is needed I also made a small bash script for setting up a database to test with:

sqlite3 testdb <<SQL_ENTRY_TAG_1
INSERT INTO tbl VALUES ("First row", 10);
INSERT INTO tbl VALUES ("Second row", 20);

After running the script and compiling the program you can access the database by e.g:

./sqlitesample2 testdb "select * from tbl"

Which will output:
data = First row
num = 10.0
data = Second row
num = 20.0

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]