0

嗨,我是 Flash builder 的新手,对编程非常生疏,我正在尝试连接到嵌入式 XML 文件并能够使用 SQLite 读取它,以便创建搜索栏过滤信息等

好吧,这是我的 DAO

package model
{
    import flash.data.SQLConnection;
    import flash.data.SQLStatement;
    import flash.filesystem.File;
    import flash.filesystem.FileMode;
    import flash.filesystem.FileStream;

    import model.Loans;

    import mx.collections.ArrayCollection;

    public class LoansDAO
    {
        private var _sqlConnection:SQLConnection;

        public function get sqlConnection():SQLConnection
        {
            if (_sqlConnection)
                return _sqlConnection;
            openDatabase(File.documentsDirectory.resolvePath("EZDemo.db"));
            return _sqlConnection;
        }

        public function findByName(searchKey:String):ArrayCollection
        {
             var sql:String = "SELECT * FROM loans WHERE loan || ' ' || companyName LIKE '%"+searchKey+"%' ORDER BY loan";
             var stmt:SQLStatement = new SQLStatement();
             stmt.sqlConnection = sqlConnection;
             stmt.text = sql;
             stmt.execute();
             var result:Array = stmt.getResult().data;
             if (result)
             {
                 var list:ArrayCollection = new ArrayCollection();
                 for (var i:int=0; i<result.length; i++)
                 {
                     list.addItem(processRow(result[i]));   
                 }
                 return list;
             }
             else
             {
                 return null;
             }
        }

        public function create(loans:Loans):void
        {
            trace(loans.loan);
            var sql:String = 
                "INSERT INTO loans (accountManager, loanManagerEMail, status, loan, companyName, dateCreated, etShipping, rfmApproval, approvalBU, approvalRFM, placementTerm, listPrice, isa, vSapOrderNum, isr, dueDate, shipDate, carrier, tracking) " +
                "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.parameters[0] = loans.loan;
            stmt.parameters[1] = loans.companyName;
            stmt.parameters[2] = loans.dateCreated;
            stmt.parameters[3] = loans.etShipping;
            stmt.parameters[4] = loans.rfmApproval;
            stmt.parameters[5] = loans.approvalBU;
            stmt.parameters[6] = loans.approvalRFM;
            stmt.parameters[7] = loans.vSapOrderNum;
            stmt.parameters[8] = loans.placementTerm;
            stmt.parameters[9] = loans.status;
            stmt.parameters[11] = loans.shipDate;
            stmt.parameters[12] = loans.carrier;
            stmt.parameters[13] = loans.tracking;
            stmt.parameters[14] = loans.listPrice;
            stmt.parameters[15] = loans.dueDate;
            stmt.parameters[16] = loans.loanManagerEMail;
            stmt.parameters[17] = loans.accountManager;
            stmt.parameters[18] = loans.isa;
            stmt.parameters[19] = loans.isr;
            stmt.execute();
            loans.loaded = true;
        }

        protected function processRow(o:Object):Loans
        {
            var loans:Loans = new Loans();
            loans.loan = o.loan == null ? "" : o.loan;
            loans.companyName = o.companyName == null ? "" : o.companyName;
            loans.dateCreated = o.dateCreated == null ? "" : o.dateCreated;
            loans.etShipping = o.etShipping == null ? "" : o.etShipping;
            loans.rfmApproval = o.rfmApproval == null ? "" : o.rfmApproval;
            loans.approvalBU = o.approvalBU == null ? "" : o.approvalBU;
            loans.approvalRFM = o.approvalRFM == null ? "" : o.approvalRFM;
            loans.vSapOrderNum = o.vSapOrderNum == null ? "" : o.vSapOrderNum;
            loans.placementTerm = o.placementTerm == null ? "" : o.placementTerm;
            loans.status = o.status == null ? "" : o.status;
            loans.shipDate = o.shipDate == null ? "" : o.shipDate;
            loans.carrier = o.carrier == null ? "" : o.carrier;
            loans.tracking = o.tracking == null ? "" : o.tracking;
            loans.listPrice= o.listPrice == null ? "" : o.listPrice;
            loans.dueDate = o.dueDate == null ? "" : o.dueDate;
            loans.loanManagerEMail = o.loanManagerEMail == null ? "" : o.loanManagerEMail;
            loans.accountManager = o.accountManager == null ? "" : o.accountManager;
            loans.isa = o.isa == null ? "" : o.isa;
            loans.isr = o.isr == null ? "" : o.isr;


            loans.loaded = true;
            return loans;
        }

        public function openDatabase(file:File):void
        {
            var newDB:Boolean = true;
            if (file.exists)
                newDB = false;
            _sqlConnection = new SQLConnection();
            _sqlConnection.open(file);
            if (newDB)
            {
                createDatabase();
                populateDatabase();
            }
        }

        protected function createDatabase():void
        {
            var sql:String = 
                "CREATE TABLE IF NOT EXISTS loans ( "+
                "loan INTEGER, " +
                "companyName VARCHAR(30), " +
                "dateCreated VARCHAR(20), " +
                "etShipping VARCHAR(20), " +
                "rfmApproval VARCHAR(20), " + 
                "approvalBU VARCHAR(3), " +
                "approvalRFM VARCHAR(3), " +
                "vSapOrderNum VARCHAR(15), " + 
                "placementTerm VARCHAR(25), " +
                "status VARCHAR(21), " +
                "shipDate VARCHAR(20), " +
                "carrier VARCHAR(20), " +
                "tracking VARCHAR(25), " +
                "listPrice VARCHAR(25), " +
                "dueDate VARCHAR(30), " +
                "loanManagerEMail VARCHAR(30), " +
                "accountManager VARCHAR(40), " +
                "isa VARCHAR(40), " +
                "isr VARCHAR(40))";
            var stmt:SQLStatement = new SQLStatement();
            stmt.sqlConnection = sqlConnection;
            stmt.text = sql;
            stmt.execute();         
        }

        protected function populateDatabase():void
        {
            var file:File = File.applicationDirectory.resolvePath("assets/loans.xml");
            var stream:FileStream = new FileStream();
            stream.open(file, FileMode.READ);
            var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
            stream.close();
            for each (var emp:XML in xml.loans)
            {
                var loans:Loans = new Loans();
                loans.loan = emp.loan;
                loans.companyName = emp.companyName;
                loans.dateCreated = emp.dateCreated;
                loans.etShipping = emp.etShipping;
                loans.rfmApproval = emp.rfmApproval;
                loans.approvalBU = emp.approvalBU;
                loans.approvalRFM = emp.approvalRFM;
                loans.vSapOrderNum = emp.vSapOrderNum;
                loans.placementTerm = emp.placementTerm;
                loans.status = emp.status;
                loans.shipDate = emp.shipDate;
                loans.carrier = emp.carrier;
                loans.tracking = emp.tracking;
                loans.listPrice = emp.listPrice;
                loans.dueDate = emp.dueDate;
                loans.loanManagerEMail = emp.loanManagerEMail;
                loans.accountManager = emp.accountManager;
                loans.isa = emp.isa;
                loans.isr = emp.isr;

                create(loans);
            }
        }

    }
}

不知道发生了什么,这没有返回任何东西

如果您需要其他东西,请告诉我,并提前感谢我能得到的任何帮助

4

1 回答 1

0

您发出的请求SQLStatement是异步的(默认情况下,Flash / AIR 中的大多数 I/O 或网络操作都是如此)。您需要为"result""fail"事件添加处理程序(请参阅此处:http ://help.adobe.com/en_US/air/reference/html/flash/data/SQLStatement.html#event:result ),并从处理程序访问数据.

于 2013-04-03T22:36:43.030 回答