dennislee
dennislee

Reputation: 65

How to return the primary key sequence number after MySQL insert

I am using flex builder 4 to write actionscript to insert a row to a table. The table has a primary key, say iddCourse with AUTO_INCREMENT. In FB4, I used to have dataservice PHP to insert row and it will return the sequence number of the newly created row, ie the generated number stored in iddCourse. For example before insert, the last sequence number recorded in iddCourse is 179, the newly created row will have the iddCourse as 180. And, I am able to get this number 180 through the ResultEvent. The actual code is as follow:

protected function createCourseHere(event:ResultEvent):void
    {
        rspCreateCourse.token = svcCourse.createCourse(voCourseSaved);
        rspCreateCourse.addEventListener(ResultEvent.RESULT, onRspCreateCourse);
    }
    
    protected function onRspCreateCourse(event:ResultEvent):void
    {   
        rspCreateCourse.removeEventListener(ResultEvent.RESULT, onRspCreateCourse);
        voCourseSaved.iddCourse = event.result as int;
        acCourse.addItem(voCourseSaved);
        acCourse = UtilArray.sortAc1(acCourse, "txtCourseCodeE", "S", "A");
        UtilMessage.sendMessage("Course created");
    }

You can see that I use event.result as int to find out the sequence number of the newly created row. Recently, I changed the data service to HTTPService instead of PHP (using ZendAMF). And now, the event.result turns out to be blank, hence unable for me to find out the latest sequence number created. Of course, I can be make another trip to the server to get the newly created row by some other key and find out the sequence number, but I don't to do so. Is there any way to find out the newly created primary key sequence number by HTTPService Data Service in actionscript from the ResultEvent?

Upvotes: 0

Views: 145

Answers (1)

Yoones Mashayekhi
Yoones Mashayekhi

Reputation: 481

I used to solve same issue with these code, maybe it helps you

var result:SQLResult = new SQLResult();
stat.clearParameters();
stat.text = "select len from property_unit_designer_temp where id=?"
stat.parameters[0] = id;
stat.execute();
result = stat.getResult();
return result.lastInsertRowID;

Upvotes: 0

Related Questions