Avg. Rating 3.4

Problem

Frequently, I keep SQL code in a file, which gets embedded into the target AIR applications. Since it is useful to keep related statements in one file, you'd might wish to execute the whole set of statements at one. Unfortunately, SQLStatement executes just the first statement.

Solution

Since multiple SQL statements are postfixed by ';', we need to split the statement sequence into parts and execute them step by step.

Detailed explanation

This small snippet does the trick by splitting the whole sequence of statements in parts. Here, the variable createSQL contains the original SQL code:

 try {
      // Separate all statements
       var parts:Array = createSQL.split( ');' );

      for( var i:int; i<parts.length; i++ ) {
         // Only, if we really have an SQL statement
         if ( '' != parts[i] ) {
             createStmt.text = parts[i] + ');';
             createStmt.execute();   
         }
      }   
   } catch( error:SQLError ) {
       // something failed...
   }

Note, that we need to postfix the partial SQL statements with ');', since we splitted the original sequence using split( ');'

Report abuse

Related recipes