Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLStatement.execute() - multiple queries in one statement

I've written a database generation script in SQL and want to execute it in my Adobe AIR application:

Create Table tRole (     roleID integer Primary Key     ,roleName varchar(40) ); Create Table tFile (     fileID integer Primary Key     ,fileName varchar(50)     ,fileDescription varchar(500)     ,thumbnailID integer     ,fileFormatID integer     ,categoryID integer     ,isFavorite boolean     ,dateAdded date     ,globalAccessCount integer     ,lastAccessTime date     ,downloadComplete boolean     ,isNew boolean     ,isSpotlight boolean     ,duration varchar(30) ); Create Table tCategory (     categoryID integer Primary Key     ,categoryName varchar(50)     ,parent_categoryID integer ); ... 

I execute this in Adobe AIR using the following methods:

public static function RunSqlFromFile(fileName:String):void {     var file:File = File.applicationDirectory.resolvePath(fileName);     var stream:FileStream = new FileStream();     stream.open(file, FileMode.READ)     var strSql:String = stream.readUTFBytes(stream.bytesAvailable);     NonQuery(strSql); }  public static function NonQuery(strSQL:String):void {     var sqlConnection:SQLConnection = new SQLConnection();     sqlConnection.open(File.applicationStorageDirectory.resolvePath(DBPATH));     var sqlStatement:SQLStatement = new SQLStatement();     sqlStatement.text = strSQL;     sqlStatement.sqlConnection = sqlConnection;     try {         sqlStatement.execute();     } catch (error:SQLError) {         Alert.show(error.toString());     } } 

No errors are generated, however only tRole exists. It seems that it only looks at the first query (up to the semicolon- if I remove it, the query fails). Is there a way to call multiple queries in one statement?

like image 890
Shawn Avatar asked Aug 01 '08 13:08

Shawn


People also ask

How do I run multiple queries in postgresql?

Batch mode You can write multiple psql commands and SQL statements in one text file (say, named statements. sql ), and then use the command psql congress -af statements. sql to execute them all. Use “ ; ” to signal the end of each SQL statement in the file.

Which method is used to execute multiple queries using a statement object stmt?

The addBatch() method of Statement, PreparedStatement, and CallableStatement is used to add individual statements to the batch. The executeBatch() is used to start the execution of all the statements grouped together.

How run multiple MySQL queries?

MySQL optionally allows having multiple statements in one statement string, but it requires special handling. Multiple statements or multi queries must be executed with mysqli::multi_query(). The individual statements of the statement string are separated by semicolon.


1 Answers

I wound up using this. It is a kind of a hack, but it actually works pretty well.

The only thing is you have to be very careful with your semicolons. : D

var strSql:String = stream.readUTFBytes(stream.bytesAvailable);       var i:Number = 0; var strSqlSplit:Array = strSql.split(";"); for (i = 0; i < strSqlSplit.length; i++){     NonQuery(strSqlSplit[i].toString()); } 
like image 169
Shawn Avatar answered Sep 23 '22 22:09

Shawn