在flex中插入多个记录

时间:2013-11-20 07:42:42

标签: actionscript-3 sqlite flex mobile

我使用以下代码将多条记录插入Sqlite数据库,但是我没有获取和记录插入。

private var planActivities:SQLStatement;
private var planActivitiesSQL:String = 'INSERT INTO DailyActivities (timestamp, activityId, activityDescription, hours, calPerMin, hoursCompleted, exercisePlanId)                              VALUES (:timestamp, :activityId, :activityDescription, :hours, :calPerMin, :hoursCompleted, :exercisePlanId)';

            private function addPlannedActivities(activities:ArrayCollection):void
        {

            var currentDate:Date = new Date();
            var numberLoops:Number = 0;

            trace("\nNumber of Activities to add " + activities.length);

            planActivities = new SQLStatement();
            planActivities.sqlConnection = model.connection;
            planActivities.text = planActivitiesSQL;
            model.connection.begin();

            for(var i:uint=0; i < activities.length; i++)
            {                   

                planActivities.parameters[":timestamp"] = currentDate;
                planActivities.parameters[":activityId"] = activities[i].activityId;
                planActivities.parameters[":activityDescription"] = activities[i].activityName;
                planActivities.parameters[":hours"] = activities[i].hours;
                planActivities.parameters[":calPerMin"] = activities[i].calPerMin;
                planActivities.parameters[":hoursCompleted"] = activities[i].totalCal;
                planActivities.parameters[":exercisePlanId"] = planId;

                planActivities.execute();                   
            }


            model.connection.commit();

            var resultCheck:SQLResult = planActivities.getResult();
            trace("\nNumber of rows affected :  " + resultCheck.rowsAffected);

            navigator.pushView(exerciseMainView, model);
        }

我有SQL结果和错误的事件监听器但是一直给我错误。使用INSERT和UNION SELECT Sqlite解决了这个问题的问题。

我想知道如何在Flex和ActionScript中完成此操作。

1 个答案:

答案 0 :(得分:0)

以下是Flash的文档类,您也可以在Flex中轻松使用它,此示例工作正常,可能会为您提供项目中的指示:

package
{

import flash.data.SQLConnection;
import flash.data.SQLMode;
import flash.data.SQLStatement;
import flash.display.MovieClip;
import flash.filesystem.File;

public class Main extends MovieClip
{
    private var conn:SQLConnection;

    public function Main()
    {
        createDatabase();
        insertMultipleData();
        showResult();
    }

    private function createDatabase():void
    {
        var dbFile:File= File.applicationStorageDirectory.resolvePath("database.db");
        var createStmt:SQLStatement;
        var sql:String ='CREATE TABLE "content" ("id"  INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"sometext" TEXT NOT NULL,"somenumber"  NUMBER NOT NULL);';

        conn = new SQLConnection();
        conn.open(dbFile, SQLMode.CREATE);

        createStmt = new SQLStatement();
        createStmt.sqlConnection = conn;
        createStmt.text = sql;
        createStmt.execute();
        createStmt = null;
    }

    private function insertMultipleData():void
    {

        var sql:String="INSERT INTO content (sometext , somenumber) VALUES (@sometext, @somenumber);"
        var someData:Array = [{text: "text1", num: 1}, {text: "text2", num: 2}, {text: "text3", num: 3}, {text: "text4", num: 4}]; // just some data to insert in the database

        var saveStmt:SQLStatement = new SQLStatement();         
        saveStmt.sqlConnection = conn;      

        conn.begin();           
        for (var i:int = 0; i < someData.length; i++)
        {
            saveStmt = new SQLStatement();

            saveStmt.sqlConnection = conn;
            saveStmt.text = sql;
            saveStmt.parameters["@sometext"] = someData[i].text;
            saveStmt.parameters["@somenumber"] = someData[i].num;
            saveStmt.execute();

        }
        conn.commit();
        saveStmt.execute();         
        saveStmt = null;
    }

    private function showResult():void
    {
        var sql:String="SELECT * FROM content";

        var selectStmt:SQLStatement = new SQLStatement();
        selectStmt.sqlConnection = conn;

        selectStmt.text = sql;
        selectStmt.execute();

        var serverResult:Object = selectStmt.getResult().data;          
        for (var i:int = 0; i < serverResult.length; i++)
        {
            trace(serverResult[i].sometext, serverResult[i].somenumber)
        }
        selectStmt = null;
    }

}

}