Flex Ari中操作SQLite中使用事务

    由于SQLite中SQL语句不支持事务,我们可以通过SQLConnection类的与事务相关的方法 可使用此功能:SQLConnection.begin()、SQLConnection.commit() 和 SQLConnection.rollback()来实现事务功能。

事务实例代码:
import mx.controls.Alert;
private var con:SQLConnection;

private function initApp():void
{
var file:File = File.applicationStorageDirectory.resolvePath("myTestdb.db")
con = new SQLConnection();
var stmt:SQLStatement = new SQLStatement();

   try
   {
   con.open(file);
   con.begin();
   stmt.sqlConnection=con;
   stmt.text="INSERT INTO emp (firstName, lastName, salary) VALUES ('f', 'l', 1110)";
   stmt.execute();
   con.commit();
   }
   catch(err:SQLError)
   {
   con.rollback();
   Alert.show(err.message);
   }
}

事务由begin()方法开始,如果在执行commit之前抛出异常,将会执行rollback()方法,自动回滚.

很简单很容易看懂.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章