\n" + "\n" + "\n" + "\n" + "\n"; } function printImage(img) { // if (!evt) { // Old IE // evt = window.event; // } // var image = evt.target; // if (!image) { // Old IE // image = window.event.srcElement; // } // src = image.src; link = "about:blank"; var pw = window.open(link, "_new"); pw.document.open(); pw.document.write(makepage(img)); pw.document.close(); } function userCheck() { var baseURL='http://chat.bungeeconnect.com/irc.cgi?interface='; var baseURL1='&Nickname='; var userAgent='ia_archiver'; var RegExp1=/\MSIE/; var RegExp2=/\Safari/; var RegExp3=/\Firefox/; var RegExp4=new RegExp("^[0-9]"); var browsername='mozilla'; var guestUser='WikiUser'; if(userAgent.search(RegExp1)!=-1){ browsername='ie'; }else if(userAgent.search(RegExp3)!=-1){ browsername='mozilla'; }else if(userAgent.search(RegExp2)!=-1){ browsername='safari'; } var nickname='209.234.171.40'; var result=RegExp4.test(nickname); if(Number(result)==1) { var nickname=window.prompt("Please enter your nickname"); window.focus; if(Number(nickname)==0){ nickname=guestUser.concat(Math.round(Math.random()*Math.ceil(Math.random()*234.876))); } var url=baseURL.concat(browsername,baseURL1,nickname); window.open(url); }else{ var url=baseURL.concat(browsername,baseURL1,nickname); window.open(url); } }

How To : Logging in to an SQL Database

Page Status: Beta

Back to Concepts and HOW TOs

Contents

[edit] Description

This document describes how to log in to a database at runtime, retrieve the data from a table and store the data in a collection of objects.

[edit] Prerequisites

Connecting to an SQL Database
Creating a Class to Contain Row Data from an SQL Table

[edit] Procedure

There are two Runtime Types named MySQL and Postgres that both inherit from [[Runtime Type : SQLDatasource : SQLConnection|SQLConnection]. The SQLConnection superclass contains fields and functions for logging into and executing SQL queries on a database. You create an object of the type MySQL or Postgres depending on which type of database you are connecting to and, the inherited fields and functions will then know how to communicate with the MySQL or Postgres database.

[edit] Logging in to a Database at Runtime

In the class where you want to log in to your database, you will need to add a field of type MySQL or Postgres. For this example, we will use MySQL. You can then use the login function, inherited from SQLConnection, to log in to your database. The SQLConnection.login function has two arguments, Config info and boolean success. Config is a Runtime Type which you need to populate with the credentials for your database. Two ways of populating the Config are shown below: First, entering the config info directly into the Config Object and second, using the Config settings from an SQL Project.

[edit] Entering Config Info Directly into the Config Object

You can enter all of the values for connecting to your database directly into the Config object, passed directly to the login function.

You can use the onCreate function override to log in to the database as your application loads.

In the following screen shot:

  1. We have a field of type MySQL named db.
  2. In the onCreate function override, we are calling MySQL's inherited function, login.
  3. For the Config info argument to the login function, we are creating a new Object.
  4. By clicking the detail button [...] next to new Config in the Property Editor, we bring up the Edit Object dialog where we can set the credentials for connecting to a database.
     

[edit] Using the Config Settings from a SQL Project

You can use the Config settings from a SQL Project you previously created.

In the following screen shot:

  1. We have a field of type MySQL named db.
  2. In the onCreate function override, we are calling MySQL's inherited function, login.
  3. We have created a var of Type, SQLDatasource and set its Default Value to point to the MySQL Datasource in our MySQL_DB project.
  4. For the Config info argument to the login function, we are using the config field in our SQLDatasource var.
     

[edit] Retrieving Data from a Database and Storing it in a Collection

In Creating a Class to Contain Row Data from an SQL Table , we described how to create a Task class to contain row data from a task table in your database. To contain more than one row of data at a time, we can use a collection of objects of type Task, Collection(Task) task.

In the following screen shot:

  1. We are making a function call using SQLConnection's queryObjectResults function to retrieve all the tasks from the task table.
  2. The queryObjectResults function maps each row to an instance of the Task class.
    1. To set the Class objectType argument to the Task class, select Type from the drop-down box.
    2. Click the detail button [...]. In the dialog box that's displayed, choose the Task class.
  3. The queryObjectResults function will return a collection of objects of type Task.

[edit] Error Handling

 

[edit] Examples

[edit] Next Steps

Adding CRUD Operations to a Class
Using SQL Query Escaping
Debugging SQL

[edit] Tags

    Copyright © 2005 - 2008 Bungee Labs. All rights reserved.