9.
Developing database applications using FireBird+SWFKit
FireBird is a great database server, moreover,
it supports embedded database. The benefits
of using FireBird:
It's very powerful, supports triggers, stored
procedures, supports far more concurrent users
than Microsoft Access. It also supports altering
table structure, dropping tables
The database can be in a single file
Very easy to deploy the applications using
FireBird. The runtime library is less than 2M.
Programming with ADO. It's very convenient
to migrate from other databases to FireBird.
It's free. If you or your customers are on
a tight budget, it's the best choice.
This article describes how to use the FireBird
embedded database in SWFKit or SWFKit Pro.
The FireBird development environment requires
the Embedded FireBird Server, the FireBird ODBC
driver and a GUI tool, we use IBOConsole as
our GUI tool for FireBird, it's a great tool.
The Embedded FireBird Server and the FireBird
ODBC driver can be found at the FireBird
official site. The IBOConsole can be downloaded
from mengoni's
site.
We've put all the necessary files together
and built a setup
file using Inno
setup. The original files can be found at
here, including
the setup script, you can create your own setup
package by modifying the setup script. Please
read the license agreements of the softwares
carefully.
Before programming with FireBird in SWFKit,
we must create a database file and create tables
in the database file. We use the GUI tool IBOConsole
to do this.
Firstly, let's launch the IBOConsole:

Then register a new server and input username
and password. The username and password will
be used to access the database files.

Create a database file "c:\student.fdb"

Create a table in the database file, the structure
of the table is
CREATE TABLE student(id INTERGER NOT NULL PRIMARY
KEY,
name VARCHAR(50) NOT NULL,
age INTERGER NOT NULL,
gender VARCHAR(50) NOT NULL,
score INTERGER NOT NULL);

We want the "id" field to be automatically
increasing. This can be done by a trigger. Firstly
we must define a generator to generates automatically
increasing numbers.

Now define a trigger to call the generator
and press the compile button to compile it

At last, let's insert some data into the table

Remember to press the commit button after insert
a line of data. The data can also be inserted
by the interactive SQL tool

The database file is Ok, it's time to create
the database application. Run swfkit and create
a new project by adding the main swf movie.
Add the database file 'c:\student.fdb' into
the attachment list, uncheck the "pack
attached files" option, for we need to
modify the database, if it's packed into the
output file, it will be read-only.
Now let's begin to program in SWFKit. In the
"Initialize" script
//Initialize
// Prepare the connection string
// The connection string of the FireBird
ODBC driver is
// DRIVER=Firebird/InterBase(r) driver;UID=username;PWD=password;DBNAME=database
file name
// See the FireBird ODBC Help file for
more information
// Note: please don't leave blanks between
the ; and param names in the connect string
// E.g. "DRIVER=Firebird/InterBase(r)
driver; UID=username; PWD=password"
will cause error
var db_name = 'DRIVER=Firebird/InterBase(r)
driver;UID=ADMIN;PWD=test;DBNAME=';
db_name += getAdditionalFile("student.fdb");
trace(db_name);
// Create an ADODB.connection object
and open the database
var conn = new ActiveXObject("ADODB.Connection");
isConn = false;
conn.ConnectComplete = function (err,
status, cnt)
{
if (typeof err != "undefined")
{
trace(err.Description);
return;
}
else
{
trace("Connect complete!");
isConn = true;
}
}
conn.Open(db_name);
if (!isConn)
{
Dialogs.msgBox("Connect database
faild!", "Error", 16);
return;
}
// Create a recordset object and open
a query
var record = new ActiveXObject("ADODB.Recordset");
record.Open('select * from "student"
order by "id"', conn, 1, 3);
record.MoveFirst();
// Save the current position of the recordset
bookmark = record.bookmark;
trace("bookmark = ", bookmark);
FlashPlayer.bindData("id", "name",
"age", "sex", "score");
function updateForm()
{
idx = record.Fields(0).Value;
FlashPlayer.name = record.Fields(1).Value.toString();
FlashPlayer.age = record.Fields(2).Value.toString();
FlashPlayer.sex = record.Fields(3).Value.toString();
FlashPlayer.score = record.Fields(4).Value.toString();
FlashPlayer.updateData(false);
}
updateForm();
wnd = getMainWnd();
wnd.onClose = do_close;
function do_close()
{
conn.Close();
}
trace(record.RecordCount);
|
The application requires the FireBird runtime
library and the FireBird ODBC Driver to run,
so we must install the runtime library and odbc
driver on the computers of the end users.
The runtime library has only one file, the
'fbembed.dll', which can be downloaded from
the FireBird
official site, or get from the deployment
package. We must rename it to 'gds32.dll'
for the FireBird ODBC Driver requires it to
be 'gds32.dll'. The odbc driver contains three
dlls, to install it, just register the 'OdbcJdbcSetup.dll',
which is one of the three dlls.
Let's we make an installer of the application
and it will install the required files for us.
Rename the 'fbembed.dll' to 'gds32.dll' and
add it into the 'Windows' folder of the attachment
list. The installer will copy it into the Windows
folder of the end users. We also add the three
dlls of the ODBC driver and the help file into
the Windows folder. Check the 'Enable the setup
program to register activex objects' option
from [Main Menu]->[View]->[Options], then
the installer will register the 'OdbcJdbcSetup.dll'
and the ODBC driver will be installed. The key
point of the deployment is to make your application
can call the "gds32.dll" and install
the ODBC driver. We put the "gds32.dll"
into the Windows folder so any application can
link to it, you can also put it in the same
folder of your exe file. The ODBC driver dlls
can be put into any folder, but the 'OdbcJdbcSetup.dll'
must be registered (registered by regsvr32.exe,
just like registering ActiveX components), or
the driver won't be installed properly.
At last, we can build and test it.
|