Get Adobe Flash player

DBHelper Class

What the DBHelper Class does is very simple. When you are retrieving data from a database or inputting data into a database, MySQL will choke on things like apostrophes and quotation marks. My method for dealing with this situation, and I'm sure it's not the only solution, is to change these characters into ASCII characters on the way to the database and then replace the ASCII code with the correct symbols on the way back from the database. I also ran into situations where an accented character was necessary, or a special symbol was employed, and this same solution worked fine. Over time I built up quite a collection of varied symbols, and, to ease updating the list, I put the whole thing into a Class. Now I only have to go to one spot to add in new characters that I run across.

What you need

You will need the DBHelper Class. Download it Here.

Usage

To use the Class you add these lines of code in your flash movie's Main Class:

import ca.xty.myUtils.DBHelper;
private var dbh:DBHelper;

First you import the class. Then you create a variable to represent the class. When you go to use the Class you simply call the desired function like so: someString = dbh.replaceFromDb(someString); . This takes any string you feed it and converts the ASCII characters into the human readable symbols you want. To send a string to the database, you just call the Classes other function: someString = dbh.replaceToDb(someString); . This replaces the offending symbols with their ASCII code equivalent before the data is entered into the database.

The DBHelper Class

    package ca.xty.myUtils {
		import flash.display.*;
		import flash.text.*;
	
	public class DBHelper extends Sprite {
		
		private var searchStr:Array = ["&", '"', "'",  '"', "'","é", "<", ">", "è", "â", "à", "ô", "É", "Î", "ç", "á", "%", """, "!", "“", "”", "(", ")", ":", "$", '"'];
		private var replaceStr:Array = ['&', '"', "'", "“", "’", "é", "<", ">", "è", "â", "à", "ô", "É", "Î", "ç", "á", "%", "®", "©", '"', '"', "(", ")", ":", "$","”"];
		
		public function DBHelper(){
			
		}
		
		public function replaceFromDb(origStr):String{
			for(var i:int = 0; i < searchStr.length; i++){
				var tempStr:String = "";
				var startIndex:int = 0;
				var searchIndex:int = 0;
	   			if(origStr.indexOf(searchStr[i]) != -1){
		   			while((searchIndex = origStr.indexOf(searchStr[i], startIndex)) != -1){
			   			tempStr += origStr.substring(startIndex, searchIndex);
			  			tempStr += replaceStr[i];
			  			startIndex = searchIndex + searchStr[i].length;
		   			}
		   			origStr = tempStr + origStr.substring(startIndex);
	   			}else{
		   			origStr = origStr;
	   			}
			}
			return origStr;
		}
		
		public function replaceToDb(origStr):String{
			for(var i:int = 0; i < replaceStr.length; i++){
				var tempStr:String = "";
				var startIndex:int = 0;
				var searchIndex:int = 0;
	   			if(origStr.indexOf(replaceStr[i]) != -1){
		   			while((searchIndex = origStr.indexOf(replaceStr[i], startIndex)) != -1){
			   			tempStr += origStr.substring(startIndex, searchIndex);
			  			tempStr += searchStr[i];
			  			startIndex = searchIndex + replaceStr[i].length;
		   			}
		   			origStr = tempStr + origStr.substring(startIndex);
	   			}else{
		   			origStr = origStr;
	   			}
			}
			return origStr;
		}
		
		
	}
}
    

The great thing about Classes is that you don't need to know what they do. You only need to know what parameters to pass them, or what functions to call, and they do all the heavy lifting. However, if you are going to write your own classes, or alter them to suit your purposes, it's handy to have some idea of what is going on.

In the DBHelper class, you are calling one of two functions - replaceFromDb() to bring code from the database into Flash; or replaceToDb() to send code to the database from Flash. You pass in a string, usually something from a text field, and these functions iterate through that string using the arrays, replacing a symbol for its ASCII equivalent or vice versa.

The private variables, searchStr and replaceStr are arrays of the symbols or ASCII code you want to search for and replace with. To build in additional symbols as you run across them in your projects, all you have to do is return to the DBHelper class and insert the new symbol and it's equivalent ASCII code in each of the corresponding private variables. Simple!

Get Adobe Flash player

Comments

No Comments