UPDATE [10/26/08]: Source of the extensions and the example as well has been moved to the project called ‘mate-examples’ on Google Code
If you are using Mate for application development based on Adobe AIR you may need an extension for using SQLite, because its in Mate currently not built in. It seems that only one extension by Miran Loncaric available, which depends on Eric Feminellas SQLService. Unfortunately this Mate extension lacks for using result or fault handlers, using prepared SQLStatements, parameters etc.
Therefore I’ve started to develop new extensions called SQLService and SQLServiceInvoker with all the needed stuff such as result and fault handling within an EventMap, (re-)using prepared statements, “simple” SQL texts, named parameters etc.
BTW: Theo Hultberg has already opened a very cool project on Google Code for free examples based on Mate, so I hope that the new extensions will be added as well.
Screen shot of the AIR example

Code example for using the extensions
Here is the most important part – the MainEventMap which is an EventMap for handling all SQLite services.
MainEventMap.mxml ( Download code )
-
<?xml version="1.0" encoding="utf-8"?>
-
<!–
-
*
-
* Mate extensions called "SQLService" and "SQLServiceInvoker" for using AIR and SQLite
-
*
-
* @author Jens Krause [ www.websector.de/blog ]
-
*
-
–>
-
<EventMap
-
xmlns:mx="http://www.adobe.com/2006/mxml"
-
xmlns="http://mate.asfusion.com/"
-
xmlns:air="de.websector.mate.extensions.air.*"
-
xmlns:manager="example.models.manager.*"
-
xmlns:data="flash.data.*">
-
<mx:Script>
-
<![CDATA[
-
import example.models.presentation.MainViewModel;
-
import example.views.MainView;
-
import example.models.domain.MainModel;
-
import example.views.GenericFaultHandler;
-
import example.events.UserEvent;
-
import example.models.vo.UserVO;
-
import com.asfusion.mate.events.UnhandledFaultEvent;
-
import mx.events.FlexEvent;
-
-
-
-
]]>
-
</mx:Script>
-
-
<Debugger
-
level="{ Debugger.ALL }"
-
/>
-
-
<air:SQLService id="sqlService"
-
databasePath="{ SQLManager.DB_PATH }"
-
/>
-
-
<manager:SQLManager id="sqlManager" />
-
-
<!–
-
Flex Events
-
–>
-
-
-
<EventHandlers type="{FlexEvent.PREINITIALIZE}">
-
<ObjectBuilder generator="{ MainModel }" />
-
</EventHandlers>
-
-
-
-
<!–
-
Create a table using SQLServiceInvokers attribute called sql to set a SQL text
-
–>
-
<EventHandlers type="{FlexEvent.APPLICATION_COMPLETE}">
-
<air:SQLServiceInvoker
-
instance="{ sqlService }"
-
sql="CREATE TABLE IF NOT EXISTS users (userId INTEGER PRIMARY KEY AUTOINCREMENT, firstName TEXT, lastName TEXT)"
-
>
-
<air:resultHandlers>
-
<EventAnnouncer
-
generator="{ UserEvent}"
-
type="{ UserEvent.GET_ALL }"
-
/>
-
</air:resultHandlers>
-
</air:SQLServiceInvoker>
-
</EventHandlers>
-
-
<!–
-
Get all stored user from database using a prepared SQLStatement, which is created by SQLManager.
-
–>
-
<EventHandlers type="{ UserEvent.GET_ALL }">
-
<air:SQLServiceInvoker
-
instance="{ sqlService }"
-
statement="{ sqlManager.getAllUsers }"
-
>
-
<air:resultHandlers>
-
<MethodInvoker
-
generator="{ MainModel }"
-
method="setUserData"
-
arguments="{ resultObject }"
-
/>
-
</air:resultHandlers>
-
</air:SQLServiceInvoker>
-
</EventHandlers>
-
-
<!–
-
Delete selected user from database using a prepared SQLStatement, which is created by SQLManager.
-
–>
-
<EventHandlers type="{ UserEvent.DELETE }">
-
<air:SQLServiceInvoker
-
instance="{ sqlService }"
-
statement="{ sqlManager.deleteUser }"
-
parameters="{[ event.userId ]}"
-
>
-
<air:resultHandlers>
-
<EventAnnouncer
-
generator="{ UserEvent}"
-
type="{ UserEvent.GET_ALL}"
-
/>
-
</air:resultHandlers>
-
</air:SQLServiceInvoker>
-
</EventHandlers>
-
-
<!–
-
Add new user to database using a prepared SQLStatement, which is created by SQLManager.
-
–>
-
<EventHandlers type="{ UserEvent.ADD }">
-
<air:SQLServiceInvoker
-
instance="{ sqlService }"
-
statement="{ sqlManager.addUser }"
-
parameters="{[ event.userVO.firstName, event.userVO.lastName ]}"
-
>
-
<air:resultHandlers>
-
<EventAnnouncer
-
generator="{ UserEvent}"
-
type="{ UserEvent.GET_ALL }"
-
/>
-
</air:resultHandlers>
-
</air:SQLServiceInvoker>
-
</EventHandlers>
-
-
<!–
-
Update selected user from database using a prepared SQLStatement, which is created by SQLManager.
-
–>
-
<EventHandlers type="{ UserEvent.UPDATE }">
-
<air:SQLServiceInvoker
-
instance="{ sqlService }"
-
statement="{ sqlManager.updateUser }"
-
parameters="{[ event.userVO.firstName, event.userVO.lastName, event.userVO.userId ]}"
-
>
-
<air:resultHandlers>
-
<EventAnnouncer
-
generator="{ UserEvent}"
-
type="{ UserEvent.GET_ALL }"
-
/>
-
</air:resultHandlers>
-
</air:SQLServiceInvoker>
-
</EventHandlers>
-
-
-
<!–
-
Handling for fault event may dispatched by SQLServiceInvoker or other services
-
–>
-
<EventHandlers type="{ UnhandledFaultEvent.FAULT }">
-
<MethodInvoker generator="{ GenericFaultHandler }"
-
method="handleFault"
-
arguments="{event.fault}" />
-
</EventHandlers>
-
-
-
-
</EventMap>
I’m a big fan of using presentation models, so you will have a further example for using it as well
. For using presentation models within Mate check out the great example called ‘document based’ by Theo Hultberg and his very detailed post on Google Code.
To-dos
- Using transactions for batch INSERT/UPDATE/DELETE operations based on the great tips using AIR and SQLite by Paul Robertson!
Download full source
Full source of the AIR example above including the new extensions called SQLService and SQLServiceInvoker MateAIRSQLiteExample.zip
[UPDATE] Full source of the extensions and the example as well has been moved to the project called ‘mate-examples’ on Google Code[/UPDATE]
Acknowledge
- Eric Feminella: AIR SQL Framework
- Peter Elst: SQLite wrapper classes
- AIR doku on Adobe: “Working with local SQL databases“
- H. Paul Robertson: “360|Flex slides for ‘AIR SQLite: An optimization conversation“


October 5th, 2008 at 12:18 pm
Wow, this is what I call real extension, mine was just one useless experiment and was never ment to be used in real applications.
I’ll try it as soon as possible and if I find something I’ll let you know at http://mate.asfusion.com/forums/topic.php?id=169
..and BTW this is mine website: http://miran.nonlogic.org/
and not http://www.freewebtown.com/mloncaric/ which shouldn’t be online at all
October 5th, 2008 at 7:50 pm
[...] Voici l’adresse : WS-Blog [...]
October 13th, 2008 at 12:30 am
Hi Jens,
It would be great if you can get in touch with Theo to get access to the new extensions repository and put the code there. (if you want, of course).
October 13th, 2008 at 8:24 pm
Of course, Laura
Theo is already contacted and within the next few days I will move all files to the mate-examples project on Google Code.
That’s why I love open source
-Jens
October 21st, 2008 at 8:34 am
This is really nice work Jens, I’m using this extension in an AIR project ATM – suggestion for you, with INSERT statements it could be useful to be able to utilise the lastInsertRowID from the SqlConnection in the SQLService class, since it’s there to save on making an extra trip to get that value – what do you think?
October 21st, 2008 at 9:29 am
Andrew, thats a good point! I will check it and update as soon as possible.
BTW: As mentioned before I’m going to move the extension to the Google Code project called “mate-examples”. So anybody who is interested can make an improvement directly. That’s called real open source
-Jens
October 23rd, 2008 at 6:53 am
Gute Arbeit Jens, great work! Looking forward to future releases. Are you planning to create a SWC as well?
Thanks! Thomas
October 23rd, 2008 at 8:16 am
Thomas, the extension will be available as SWC at mate-examples too
.
-Jens
November 20th, 2008 at 5:02 pm
Hello!
thanks for this extension.
is there a way to share a sqlService between multiple EventMaps?
January 7th, 2009 at 2:39 pm
[...] …to dive into Swiz. So I ported my latest published example using Mate into Swiz. [...]
January 26th, 2009 at 9:31 pm
The google code page does *not* have the source code available anymore. Can this be remedied?
January 26th, 2009 at 9:33 pm
Actually it only seemed to work by taking the link to it from the project description page. Going to the source tab by itself never worked
January 26th, 2009 at 10:33 pm
Real question: Do you plan on adding support for Async connections?
January 27th, 2009 at 3:04 pm
@Jon: At the moment I don’t plan to add an asynchronous support.
Have you tried to checkout the source using SVN? It should be available using the following command:
svn checkout http://mate-examples.googlecode.com/svn/trunk/ mate-examples-read-only.Or you can check the code using your Browser (without Google’s interface): http://mate-examples.googlecode.com/svn/trunk/examples/airsqlite/
-Jens
January 29th, 2009 at 11:35 pm
Hi Jens
Have you thought to update the extension to include an argument for the encryptionKey parameter for the connections open() method for encrypted databases for AIR 1.5?
I’m hacking the original source for now for an application I’m using your extension on.
Thanks, Andrew
January 30th, 2009 at 11:26 am
@Andrew: At the moment I don’t plan any improvements on this extensions.
Feel free to add your “hack” to the original extension on Google Code. It’s open source
Thanks!
-Jens
February 2nd, 2009 at 2:45 pm
When I try to start the application it quits without even showing the main screen… I’m using AIR 1.5. Any idea?
February 4th, 2009 at 3:27 pm
Thank you for this great example..
Regarding the SQLManager .. how would I define the sql if I wanted to use WHERE .. LIKE instead of = ?
Tried adding % to the string but I always get errors.
Thanks in advance!
February 5th, 2009 at 9:51 am
@jurgen: Have you tried something like this?
sql = "SELECT anyvalue FROM anytable WHERE anyvalue LIKE 'a%'"Note the ‘ ‘ for the wildcard definition within the SQL statement.
-Jens
February 17th, 2009 at 4:09 am
Interesting stuff! Just got into Mate today after using UM Cairngorm for the past 6 months. I too plan to have a go at hacking your code to put in the transaction management, which is really easy. I don’t think Mate’ll be appropriate for the sorts of things I’m doing with SQLite, but an interesting challenge nonetheless.
I know it’s possible, but I’m yet to figger out why; It doesn’t seem like leaving the connection open from multiple operations is actually any faster than closing it every time. I was testing it on a coupla hundred inserts/deletes rather than selects (before I had transactions working), so maybe there’s benefit purely on selects…?
May 3rd, 2009 at 5:19 am
@jurgen The problem is using the ‘?’ index parameter next to the ‘%’ character – that’s why the error keeps getting thrown.
The solution is to concatenate the parameter with the character like this, where ‘||’ is the concatenation operator in SQLite:
sql = “SELECT anyvalue FROM anytable WHERE anyvalueLIKE ? || ‘%’”
Andrew
August 8th, 2009 at 10:02 am
[...] http://www.websector.de/blog/2008/10/04/new-mate-extensions-for-using-air-and-sqlite-sqlservice-sqls... Source: [...]
May 3rd, 2010 at 4:47 pm
hi guys…
hi guysI would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well and i have start my own blog now, , thanks for your effort…