Monday, February 9, 2009

How to escape characters while Saving Data in SQLITE database

I have used the following function while saving data to sqllite if the data had some characters which needed to be escaped passing the string to the function returns the escapes string .

private static function SQLSafe(strTemp:String):String
{
var i:Number = 0;
var iOld:Number = 0;
var firstQuote:Boolean = false;
var strNew:String = "";
strTemp = StringUtil.trim(strTemp);
while (i != -1){
i = strTemp.indexOf("'", i);
if (i != -1){
if ((strNew != "") || (firstQuote)){
strNew += "'" + strTemp.substring(iOld, i);
}
else if (i != 0) {
strNew = strTemp.substring(iOld, i);
}
else {
firstQuote = true;
}
iOld = i;
i++;
}
}
if (iOld <= strTemp.length){
if (strNew != ""){
strNew += "'" + strTemp.substring(iOld, strTemp.length);
}
else {
strNew = strTemp.substring(iOld, strTemp.length);
}
}
return (strNew);
}


Cheers

Varun Rathore

No comments:

About Me