2012-02-04 85 views
0

我想從列表控件中刪除選定的項目,但不能。這有什麼錯我的代碼:Flex SQLite刪除功能

[Bindable]private var dp:ArrayCollection = new ArrayCollection(); 
private var conn:SQLConnection; 

protected function Delete(event:MouseEvent):void { 
    Stmt = new SQLStatement(); 
    Stmt.sqlConnection = conn; 
    Stmt.text = "DELETE FROM UserTable WHERE firstName="+listBox.selectedIndex; 
    Stmt.execute(); 
} 

<s:List id="listBox" itemRenderer="UserRenderer"></s:List> 



In UserRenderer: 

<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark"> 

    <s:Label text="{data.lastName}, {data.firstName}, {data.id}"/> 

</s:ItemRenderer> 

回答

3

List#selectedIndex是指選定項目的dataProvider中的位置。第一個元素的索引爲0,第二個索引爲1,依此類推。如果沒有選擇項目,selectedIndex將是-1

如果你想選擇或刪除firstName - 就像你在查詢中做的那樣 - 你將不得不傳入一個有效的名字而不是索引位置。你可以用List#selectedItem屬性來做到這一點。如果不使用查詢參數,也不要忘記查詢中的單引號。

"DELETE FROM UserTable " + 
"WHERE firstName = '" + listBox.selectedItem.firstName + "'"; 

你沒有要求這一點,但無論如何,我會告訴你:在查詢中使用的變量時,你應該使用查詢參數安全原因。在ActionScript實現這一目標的一個方法是:(這裏不需要單引號)

stmt.parameters[0] = listBox.selectedItem.firstName; 
stmt.text = "DELETE FROM UserTable WHERE firstName = ?"; 

+0

感謝您的快速回復,刪除工作! – jameslcs 2012-02-04 22:48:22

+0

感謝您的安全信息,我會嘗試在我的代碼中使用查詢參數。 (根據你的代碼在stmt.text關閉「報價添加) – jameslcs 2012-02-04 23:06:52

+0

@jameslcs修正了錯字,感謝您指出 – RIAstar 2012-02-04 23:13:58