How to insert data from dynamic array to sqlite db

2019-03-04 20:02发布

问题:

How can I insert dynamic Array to data base using Loop at the success of the Webservice call

first my Webservice which i have Bind to DataGrid

<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>

This WebService Is called when i clicked on my Loging Button with the help Of the function

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

My Login() FUnction is

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);
        }

At the Result Event i am calling the Another webservice Method

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);
            }

and the Definition of the InsertUser Function is

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();

}

The Following process i am doing To insert into my db but there is an error , Only First Row returned from the WebService is Inserted into Table

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]

but i am getting errors while In the SUccess Event of the CallResponder Related to the Sql Execution In For Loop

Could anyone help me to insert the Array to Database ... Remember my Array is dynamic The proper Sequence of the functions i have discribed here ... if anybody can help me to Insert Into Table in local database Using Forloop .

Any Help Will be greatly Admired .. Thanks..

回答1:

My code was absolutely Right but a silly mistake done while calling the Database .
I just changed the call method

conn.openAsync(db); To conn.open(db); And it worked

    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);
            } 

Thanks to http://www.linkedin.com/in/tomvandeneynde