簡體   English   中英

如何在ActionScript 3.0和Flex中使用Loop將動態數組中的數據從WebService插入到sqlite db

[英]How to insert data from dynamic array to sqlite db getting from WebService Using Loop in action script 3.0 and flex

Webservice調用成功后,如何使用Loop將動態數組插入數據庫

首先是我綁定到DataGrid的Web服務

<fx:Declarations>

        <mx:WebService 
            id="ws" 
            wsdl="http://localhost:2690/vtrServices.asmx?wsdl"> 
        </mx:WebService>

        <vtrservices:VtrServices id="vtrServices"
                                 fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)"
                                 showBusyCursor="true"
                                 result="vtrServices_resultHandler(event)"
                                 />

        <s:CallResponder id="SignIn1Result2"/>
        <s:CallResponder id="GetMyTasksNew1Result"
                     result="GetMyTasksNew1Result_resultHandler(event)"/>


    </fx:Declarations>

當我在函數的幫助下單擊“日志記錄”按鈕時,將調用此WebService

<mx:Button label="Login" id="btnLogin" click="Login();"/>

我的Login()功能是

private function Login():void {
                // Get Data from WebService and fill datagrid when you fist invoke the application
                SignIn1Result2.token = vtrServices.SignIn1(txtUserName.text, txtPassword.text);
                stmt.sqlConnection = this.isDbConnected(conn);
        }

結果事件中,我正在調用另一個Web服務方法

protected function vtrServices_resultHandler(event:ResultEvent):void
            {
                // TODO Auto-generated method stub
                InsertUser(SignIn1Result2.lastResult[0].UserId,SignIn1Result2.lastResult[0].UserName,SignIn1Result2.lastResult[0].ContactName,SignIn1Result2.lastResult[0].Password);
                UserLogin.visible= false;
                GetMyTasksNew1Result.token = ws.GetMyTasksNew1(SignIn1Result2.lastResult[0].UserId);
            }

並且InsertUser函數的定義是

private function InsertUser(UserId:String, UserName:String,ContactName:String, Password:String):void
        {
            stmt.sqlConnection = this.isDbConnected(conn);
            stmt.text = "INSERT OR REPLACE INTO TblUsers (UserId, UserName, ContactName ,Password) VALUES('"+UserId+"','"+UserName+"','"+ContactName+"','"+Password+"');";  
            stmt.execute();
            stmt1.sqlConnection = this.isDbConnected(conn);
            stmt1.text = "CREATE TABLE IF NOT EXISTS TblTasks (TaskId INTEGER PRIMARY KEY, AssignmentId INTEGER, ProjectId INTEGER,TeamId INTEGER,AssigneeName Varchar(100),Priority Varchar(15),ActualStartTime DATETIME,ActualEndTime DATETIME,Progress INTEGER);";
            stmt1.execute();

}

我正在執行的以下過程要插入到數據庫中,但有一個錯誤,只有從WebService返回的第一行插入到表中

protected function GetMyTasksNew1Result_resultHandler(event:ResultEvent):void
            {
                stmt2.sqlConnection = this.isDbConnected(conn);         
                 //Alert.show(GetMyTasksNew1Result.lastResult[2].TaskId.toString());
                 for(var i:int=0;i<=GetMyTasksNew1Result.lastResult.length-1;i++)
                {  
                Alert.show(GetMyTasksNew1Result.lastResult[i].TaskId.toString());
                stmt2.text = "Insert OR REPLACE INTO TblTasks (TaskId, AssignmentId , ProjectId ,TeamId ,AssigneeName ,Priority ,ActualStartTime ,ActualEndTime ,Progress ) Values('"+GetMyTasksNew1Result.lastResult[i].TaskId+"','"+GetMyTasksNew1Result.lastResult[i].AssignmentId+"','"+GetMyTasksNew1Result.lastResult[i].ProjectId+"','"+GetMyTasksNew1Result.lastResult[i].TeamId+"', '"+GetMyTasksNew1Result.lastResult[i].AssigneeName+"','"+GetMyTasksNew1Result.lastResult[i].Priority+"','"+GetMyTasksNew1Result.lastResult[i].StartTime+"', '"+GetMyTasksNew1Result.lastResult[i].EndTime+"','"+GetMyTasksNew1Result.lastResult[i].Progress+"');"; 
                stmt2.execute(); 
                 } 



            }

            ![Data i want to insert it is dynamic for every User][1]

但是我在與For循環中的Sql執行有關的CallResponder成功事件中遇到錯誤

誰能幫助我將數組插入數據庫... 記住我的數組是動態的我在這里描述的函數的正確順序...如果有人可以幫助我使用Forloop將其插入本地數據庫中的表中。

任何幫助將不勝感激..謝謝..

我要插入的屏幕截圖值和錯誤

在此處輸入圖片說明

我的代碼絕對正確,但是在調用數據庫時犯了一個愚蠢的錯誤。
我只是改變了通話方式

conn.openAsync(db); conn.open(db); 而且有效

    private function dbinit(event:Event):void
            {
                var dir:File = File.applicationDirectory;
                //
                var db:File = dir.resolvePath("dbImthePM.db");
                // after we set the file for our database we need to open it with our SQLConnection.

                conn.open(db);
/*              conn.openAsync(db); */ //Earlier It was Not letting me to reuse the sql statments
                //We set event listeners to check that the database is opened
                //The second event listener is to catch any processing errors
                //The last is handle the results from our queries since
                //Actionscript is an event based language.
                conn.addEventListener(SQLEvent.OPEN, dbLoaded);
                conn.addEventListener(SQLErrorEvent.ERROR, sqlError);
                conn.addEventListener(SQLEvent.RESULT, sqlResult);
            } 

感謝http://www.linkedin.com/in/tomvandeneynde

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM