Example SQLite database

In a previous example we used a simple flat-file 'database'. An SQLite database is a database but the lite version. It's the local database for a lot of (mobile)apps.

We will be using SPOD macros Check more info in the about.

The code used in this example is here.

How to start

Create a folder named foobar (please use a better name; any name will do) and create folders bin and src. See example below:

+ foobar
    + bin
    + src
        - Main.hx
    - build.hxml

The Main.hx

This example is getting to big to post here, so if you want to check out the complete file go and check out Main.hx

First we need a database, so I wrote a class that creates one for you: DBStart.hx. This class generates random users.

    // Open a connection
    var cnx = sys.db.Sqlite.open("mybase.ddb");

    // Set as the connection for our SPOD manager
    sys.db.Manager.cnx = cnx;

    // initialize manager
    sys.db.Manager.initialize();

    // Create the "user" table
    if ( !sys.db.TableCreate.exists(User.manager) ) {
        sys.db.TableCreate.create(User.manager);
    }

    // Fill database with users
    for (i in 0 ... 10) {
        var user = createRandomUser();
        user.insert();
    }

    // close the connection and do some cleanup
    sys.db.Manager.cleanup();

    // Close the connection
    cnx.close();

The function createRandomUser() does what you would expect, if you want to know, check the source code.

A user! We have used a typedef before, this looks similar. The strange stuff here are the types, they are not the default types that Haxe uses. Read more about that: creating-a-spod.

import sys.db.Types;

class User extends sys.db.Object {
    public var id : SId;
    public var name : SString<32>;
    public var birthday : SDate;
    public var phoneNumber : SNull<SText>;
}

Now we have a database, lets check out the code to get the data from the database:

Main.hx

    // Open a connection
    var cnx = sys.db.Sqlite.open("mybase.ddb");

    // Set as the connection for our SPOD manager
    sys.db.Manager.cnx = cnx;

    // initialize manager
    sys.db.Manager.initialize();

    for (i in 0 ... User.manager.all().length) {
         var _user = User.manager.get(i);
         if(_user != null) trace(_user.name);
    }

    // close the connection and do some cleanup
    sys.db.Manager.cleanup();

    // Close the connection
    cnx.close();

The Haxe build file, build.hxml

There are a lot of different arguments that you are able to pass to the Haxe compiler. Set these arguments into a text file of one per line with the extension hxml. This file passes it directly to the Haxe compiler as a build script.

# // build.hxml
-cp src
-main Main
-php bin/www
-dce full

Build PHP with Haxe

To finish and see what we have, build the file and see the result

  1. Open your terminal
  2. Open the correct folder with cd where you have saved the build.hxml
  3. Type haxe build.hxml
  4. Press enter

And if everything went according to plan, you should see something simular like this:

You could build everything directly in the terminal.

haxe -cp src -main Main -php bin/www -dce full

It will have the same result

More info

results matching ""

    No results matching ""