Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Working with webSQL

Posted on 08.01.2013 by Jon Lennart Posted in Developers log, News

In the current release of Smart Mobile Studio we have finally added database support. Since smart mobile development targets the browser API directly, the classes are more low-level than what we have in delphi, but the flip side is that there are a lot less elements involved and they are very easy to use.

Creating a database, a preview

So, how do we setup a database? First we start by including the database unit, which in this case is: w3dbsql. In this example we are going to use the wrapper for webSQL, which (behind the scene) is basically sqlite built into the browser. This database is supported by Safari mobile, Safari desktop, google chrome and opera. So let’s setup a database:

Procedure TForm1.HandleButtonClick(Sender:TObject);
var
  x:  Integer;
Begin
  (* create / open our database *)
  if not FDB.Active then
  Begin
    try
      FDB.DBDescription:='My Cool Database';
      FDB.DBName:='myDB';
      FDb.Active:=True;
    except
      on e: exception do
      showmessage(e.message);
    end;
  end;

  (* Create our customer table *)
  mTrans:=TW3WebSQLWriteTransaction.Create(FDB);
  try
    mTrans.Execute("create table if not exists customers
      (id integer primary key asc,
       name string);",[]);
  finally
    if mTrans.LastFailed then
    writeln(mTrans.LastError);
    mTrans.free;
  end;

  (* populate with dummy data *)
  mTrans:=TW3WebSQLWriteTransaction.Create(FDB);
  try
    for x:=1 to 10 do
    mTrans.Execute('insert into customers
      (name) values (?);',['john doe #' + IntToStr(x)]);
  finally
    if mTrans.LastFailed then
    writeln(mTrans.LastError);
    mTrans.free;
  end;

  (* populate with dummy data *)
  mTrans:=TW3WebSQLReadTransaction.Create(FDB);
  try
    mTrans.OnSuccess:=DBCallback;
    mTrans.Execute('select * from customers;',[]);
  finally
    if mTrans.LastFailed then
    writeln(mTrans.LastError);
  end;
end;

The code above is (as the name implies) the event for a button click. We start off by checking if the database instance is active, if it’s not and it’s the first click, then we just open it. The cool part is that this will automatically create a database for us. So if the database exists, it will be opened. If it does not exist, an empty database is created.

Next is creating a table. We use a class called TW3WebSQLWriteTransaction to do the job. This is the class you use to execute queries that does not return data. In other words, creating tables, inserting records and so on.

  (* Create our customer table *)
  mTrans:=TW3WebSQLWriteTransaction.Create(FDB);
  try
    mTrans.Execute("create table if not exists customers
      (id integer primary key asc,
       name string);",[]);
  finally
    if mTrans.LastFailed then
    writeln(mTrans.LastError);
    mTrans.free;
  end;

We use the same TW3WebSQLWriteTransaction class to populate the table we just created (and as the SQL shows, its only created if the table does not exist). Again this is a fairly straight forward mechanism.

  (* populate with dummy data *)
  mTrans:=TW3WebSQLWriteTransaction.Create(FDB);
  try
    for x:=1 to 10 do
    mTrans.Execute("insert into customers
      (name) values (?);",['john doe #' + IntToStr(x)]);
  finally
    if mTrans.LastFailed then
    writeln(mTrans.LastError);
    mTrans.free;
  end;

Once populated we want to read the data back, just to make sure it’s actually there. To read data rather than write, we use an instance of TW3WebSQLReadTransaction. In order to get access to the data, we have to use an event. Everything in browsers is async (non blocking) so you also have to be careful that you don’t free the object before the callback has completed. This is what the callback event looks like:

Procedure TForm1.DBCallback(Sender:TObject);
var
  mTemp:TW3WebSQLReadTransaction;
  x:  Integer;
  mRow: Variant;
Begin
  mTemp:=TW3WebSQLReadTransaction(sender);
  if assigned(mTemp) then
  Begin
    if mTemp.Dataset<>NIL then
    Begin
      for x:=0 to mTemp.Dataset.rows.length-1 do
      Begin
        mRow:=mTemp.dataset.rows.item(x);
        writeln(mRow['name']);
      end;
    end;
  end;
end;

In this code we just dump everything to the console, so you wont see anything unless you run it with debugging on (if you use an external browser). We could easily put the info into a list, a combobox or anything else you wanted.

« Introducing a playful 2013 :-)
The Smart Contest 2013 – Topic for the first round »

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS