I have been doing some work with AIR and SQLite recently and came across and issue that had to be solved. The issue was that most everything is a VARCHAR in SQLite and I was not able to insert apostrophes (’) into any string field because that is the string delimiter.
A quick Google search revealed that escaping an apostrophe is as simple as putting two of them together (i.e. “Jake’s Blog” becomes “Jake’’s Blog”). Quotation marks remain the same.
Now that I knew what the solution was, I had to implement it. I had about 10 custom VO objects, all with different numbers and kinds of properties. I needed a way to dig into each of them and do a quick string replace on String properties only.
I was able to make use of flash.utils.describeType and XML/XMLList to make this happen. Here’s the function:
public function escapeStrings( value:Object ):Object
{
// get object details
var objectDescription:XML = flash.utils.describeType(value);
// get object properties as XMLList
var objectProperties:XMLList = new XMLList(objectDescription.accessor);
// create a new object to store modified data in
var newVal:Object = new Object();
//
// loop over all properties
for( var x:uint = 0; x < objectProperties.length(); x++ )
{
// if the property is a string, escape single ticks
if( objectDescription.accessor[x].@type == "String" )
{
newVal[objectDescription.accessor[x].@name] = String(value[objectDescription.accessor[x].@name]).replace('\",'\'\");
}
// otherwise just grab the value of the property as it is
else
{
newVal[objectDescription.accessor[x].@name] = value[objectDescription.accessor[x].@name];
}
}
// return new value
return newVal;
}
Basically, grab the XML returned from flash.utils.describeType and then create an XMLList of the accessors (properties). From there it’s a simple loop over that XMLList and a string replace.
WAIT! This is returning an Object, what happened to my custom Class?
That is one thing that I felt was not worth the time in the scope of this project to actually return the correct data type. Here is a quick way around this…
var myVO:PositionVO = new PositionVO( "W", "Waiter", null );
var o:Object = escapeStrings( myVO );
myVO = new PositionVO( o.abbreviation, o.label, o.positionVO );
Finally, here’s what the class looks like if you didn’t have an idea from the code above:
package test
{
[Bindable]
public class PositionVO
{
public var abbreviation:String;
public var label:String;
public var positionID:String;
// constructor
public function PositionVO ( abbreviation:String,label:String,positionID:String)
{
this.abbreviation = abbreviation;
this.label = label;
this.positionID = positionID;
}
}
}

Related Articles
1 user responded in this post
When I ran into the single quote problem, I cracked it by using prepared statements. This is analogous to cfqueryparam in ColdFusion. Here is a short article I wrote on how to get it to work. http://www.nodans.com/index.cfm/2007/10/31/Fixed-error-in-SQLLite-Administration-tool
Leave A Reply