从sql数据中检索,adobe air

时间:2014-11-17 20:09:55

标签: actionscript-3 flash air

我正在尝试从我能够使用SqlConnection创建的列表中检索数据。

我了解如何通过从列表框(组件)中选择项目来重新填充文本框。但是,如何通过使用单独的按钮或时间轴事件获取数据,并填写文本框。 t_fname t_lname t_phone

var connection:SQLConnection;

openDatabase();
t_phone.restrict = "0-9";

function openDatabase():void
{
var dbFile:File = File.applicationStorageDirectory.resolvePath("database.db");
connection = new SQLConnection();
connection.addEventListener(SQLEvent.OPEN, onOpen);
connection.openAsync(dbFile, SQLMode.CREATE);
}

function onOpen(SQLEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "CREATE TABLE IF NOT EXISTS contacts (id INTEGER PRIMARY KEY AUTOINCREMENT, fname TEXT, lname TEXT, phone INTEGER)";
stat.execute(-1, new Responder(selectItems));
}

function selectItems(SQLEvent):void
    {
    b_save.enabled = false;
    b_delete.enabled = false;
    var stat:SQLStatement = new SQLStatement();
    stat.sqlConnection = connection;
    stat.text = "SELECT id, fname, lname, phone FROM contacts ORDER BY id";
    stat.execute(-1, new Responder(onSelected));
    }

function onSelected(evt:SQLResult):void
{
if (evt.data != null)
{
itemList.dataProvider = new DataProvider();
for (var i:int=0; i<evt.data.length; i++)
{
itemList.addItem({label:(evt.data[i].fname + " " + evt.data[i].lname), data:evt.data[i]});
}
}
}

b_new.addEventListener(MouseEvent.CLICK, createNew);

function createNew(MouseEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "INSERT INTO contacts (fname, lname, phone) VALUES (@fname, @lname, @phone)";
stat.parameters["@fname"] = t_fname.text;
stat.parameters["@lname"] = t_lname.text;
stat.parameters["@phone"] = t_phone.text;
stat.execute(-1, new Responder(selectItems));
t_fname.text = t_lname.text = t_phone.text = "";
}

itemList.addEventListener(Event.CHANGE, onChange);

function onChange(evt:Event):void
{
b_save.enabled = true;
b_delete.enabled = true;
t_fname.text = evt.target.selectedItem.data.fname;
t_lname.text = evt.target.selectedItem.data.lname;
t_phone.text = evt.target.selectedItem.data.phone;

}

b_save.addEventListener(MouseEvent.CLICK, saveThis);

function saveThis(MouseEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "UPDATE contacts SET fname=@fname, lname=@lname, phone=@phone WHERE id=" + itemList.selectedItem.data.id;
stat.parameters["@fname"] = t_fname.text;
stat.parameters["@lname"] = t_lname.text;
stat.parameters["@phone"] = t_phone.text;
stat.execute(-1, new Responder(selectItems));
t_fname.text = t_lname.text = t_phone.text = "";
}

b_delete.addEventListener(MouseEvent.CLICK, deleteThis);

function deleteThis(MouseEvent):void
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = connection;
stat.text = "DELETE FROM contacts WHERE id=" + itemList.selectedItem.data.id;
stat.execute(-1, new Responder(selectItems));
t_fname.text = t_lname.text = t_phone.text = "";
}

////     ?how would I do it for this recall button ?

b_recall.addEventListener(MouseEvent.CLICK, recall1);

function recall1(MouseEvent):void
{

t_fname.text =  data from the first entry
t_lname.text = data from the first entry
t_phone.text = data from the first entry
}

1 个答案:

答案 0 :(得分:0)

您必须存储数据。学会解决不存储数据的困难。这是添加到代码中的基本代码。第一行是您现有的代码,在其下方添加新代码。

t_phone.restrict = "0-9";
var aItems:Array;
var oItem:Object;
var i:uint;

第一行是代码的另一行,添加下面的代码 - 抱歉不清楚:

itemList.addItem({label:(evt.data[i].fname + " " + evt.data[i].lname), data:evt.data[i]});
//  add item to end of aItems array
oItem = new Object();
oItem.t_id = evt.data[i].itemID;
oItem.t_fname = evt.data[i].fname;
oItem.t_fname = evt.data[i].lname;
oItem.t_phone = evt.data[i].phone;
aItems.push(oItem);

然后你可以遍历你的数组并查找匹配的id或其他任何内容。

//  loop through aItems to find match for t_id
for (var i:int = 0; i < aItems.length; i++) {
    if(aItems[i].t_id == testID){
        trace("f_name: " + aItems[i].t_fname);
    }
}