您的位置:首页 > 数据库

AIR 操作 sqlite 测试

2009-06-22 15:25 225 查看
demo: http://www.sandy1219.com/air/localDatabase.rar
package
{
import flash.data.SQLConnection;
import flash.data.SQLMode;
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.errors.SQLError;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;

import mx.collections.ArrayCollection;
import mx.controls.Alert;

public class SqlManager
{
private static var _instance:SqlManager;
public static function getIntance():SqlManager
{
if(_instance == null)
{
_instance = new SqlManager();
}
return _instance;
}

public static var dataList:ArrayCollection = new ArrayCollection();;

private var conn:SQLConnection;

public function connect():void
{
conn = new SQLConnection();
conn.addEventListener(SQLEvent.OPEN , openHandle);
conn.addEventListener(SQLErrorEvent.ERROR , errorHandle);

var db:File = File.applicationDirectory.resolvePath('flex.db');trace(db.url)
conn.open(db,SQLMode.UPDATE,'false');
}

private function openHandle(e:SQLEvent):void
{
trace("the database was opened successfully");
}

private function errorHandle(event:SQLErrorEvent):void
{
trace("Details:", event.error.message);
}

public function insertData():void
{
try
{
var stmt:SQLStatement = new SQLStatement();
stmt.addEventListener(SQLEvent.RESULT , insertResult);
stmt.addEventListener(SQLErrorEvent.ERROR , errorResult);
stmt.sqlConnection = conn;
stmt.text = "INSERT INTO flex ('name') VALUES ('"+ String(Math.random()*10000).slice(0,8) +"')";
stmt.execute();
}
catch(error:SQLError)
{
trace(error.toString())
Alert.show(error.details);
}
}

private function insertResult(e:SQLEvent):void
{
trace(e.toString())
var stmt:SQLStatement = SQLStatement(e.currentTarget);
var result:SQLResult = stmt.getResult();
var primaryKey:Number = result.lastInsertRowID;
trace('insert data at: ' + primaryKey)

reflash();
}

private function errorResult(e:SQLErrorEvent):void
{
trace(e.toString())
}

public function reflash():void
{
try
{
var stmt:SQLStatement = new SQLStatement();
stmt.addEventListener(SQLEvent.RESULT , reflashResult);
stmt.addEventListener(SQLErrorEvent.ERROR , reflasError);
stmt.sqlConnection = conn;
stmt.text = "SELECT * from flex";
stmt.execute();
}
catch(error:SQLError)
{
trace(error.toString())
}
}

private function reflashResult(e:SQLEvent):void
{
var res:SQLResult = SQLStatement(e.currentTarget).getResult();

dataList.removeAll();

var ls:Array =  res.data  ;
for(var i:int=0;i<ls.length;i++)
{
trace(ls[i])
dataList.addItem(ls[i]);
}
}

private function reflasError(e:SQLErrorEvent):void
{
trace(e.toString())
}

public function delItem(id:*):void
{
try
{
var stmt:SQLStatement = new SQLStatement();
stmt.addEventListener(SQLEvent.RESULT , delResult);
stmt.addEventListener(SQLErrorEvent.ERROR , delError);
stmt.sqlConnection = conn;
stmt.text = "delete from flex where id = " + id;
stmt.execute();
}
catch(error:SQLError)
{
trace(error.toString())
}
}

private function delResult(e:SQLEvent):void
{
trace(e.toString())
var stmt:SQLStatement = SQLStatement(e.currentTarget);
var result:SQLResult = stmt.getResult();
var primaryKey:Number = result.lastInsertRowID;
trace('insert data at: ' + primaryKey)

reflash();
}

private function delError(e:SQLErrorEvent):void
{
trace(e.toString())
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息