Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Connecting to the database


In our main function, let's initiate the connection to our database and the SPOD system:

import Post;

class Main
{   
   public static function main()
   {
      //Parameters to connect to the MySQL database
      var cnx = neko.db.Mysql.connect({ 
            host : "localhost",
            port : 3306,
            database : "myBlog",
            user : "root",
            pass : "",
        });
      
      //Initialize the SPOD system
        neko.db.Manager.cnx = cnx;
        neko.db.Manager.initialize();


      //We've done our processing, let's clean things and disconnect
        neko.db.Manager.cleanup();
        cnx.close();
   }
}

When doing this, we can successfully connect to the database although it won't do anything at the moment.

Now, let's just retrieve our posts from the database by simply adding this:

var posts = Post.manager.all();

We now have the following:

import Post;

class Main
{   
   public static function main()
   {
  ...