How to execute lots of SQL statement all together?
You can use Transaction to protect your SQL statement from SQL injection and to store different data in the same SQL statement and then execute it.
One of the most important things when you work with SQLite and AIR is the capability to use the same SQL statement to populate a database or a part of it.
To do this you can use Transaction in SQL statement.¿Also, transactions save your SQL statement from possible SQL injection.
To use transaction you must work with SQL parameters that are place-holder informations in SQL statement, to set parameters in SQL statement you can use ":" or "@" and a parameter's name, if you have unnamed parameters or index based array, you can use "?" instead of ":" or "@".
Following an Actionscript and Javascript sample:
Actionscript example:
var sqlStat:SQLStatement = new SQLStatement();
sqlStat.sqlConnection = dbConn;
sqlStat.text = "INSERT INTO Software VALUES(null, :name, :price, :descr)";
sqlStat.parameters[":name"] = "Adobe Flex";
sqlStat.parameters[":price"] = "free for student";
sqlStat.parameters[":descr"] = "best software of the world";
sqlStat.execute()
Javascript example:
st = new air.SQLStatement();
st.sqlConnection = dbConn;
st.text = "INSERT INTO Software VALUES(null, ?, ?, ?)"
st.parameters[0] = 'Adobe Flex';
st.parameters[1] = 'free for student';
st.parameters[2] = 'best software of the world';
st.execute()
So if you want populate a database or a part of it, you can use Transactions, you can decide to launch all datas on time with commit() SQLConnection method, but it could be dangerous if software loses database connection or there are an error in SQL statement that stop to write all
data into database.
To solve this, you can create a number of SQL statement object that refer to the same database connection so every object has his way and don't be connected to the others.
Following you can find two samples, first one for Actionscript and second one for Javascript:
Actionscript example:
for(var i:int = 0; i < dataDG.dataProvider.length; i++){
var sqlStat:SQLStatement = new SQLStatement();
sqlStat.sqlConnection = dbConn;
sqlStat.text = "INSERT INTO Software VALUES(null,?, ?, ?)";
sqlStat.parameters[0] = dataDG.dataProvider[i].name;
sqlStat.parameters[1] = dataDG.dataProvider[i].price;
sqlStat.parameters[2] = dataDG.dataProvider[i].descr;
sqlStat.execute()
}
Javascript example:
for(var i = 0; i < 500; i++){
st = new air.SQLStatement();
st.sqlConnection = dbConn;
st.text = "INSERT INTO Software VALUES(null, ?, ?, ?)"
st.parameters[0] = 'software' + i;
st.parameters[1] = i * 2;
st.parameters[2] = 'best software of the world';
st.execute()
}
In attachment you can find full HTML/Javascript and Actionscript examples.
+