[英]Flex SQLite delete function
I wanted to delete a selected Item from a List control but can't. 我想从列表控件中删除选定的项目,但不能删除。 What's wrong with my code:
我的代码有什么问题:
[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>
List#selectedIndex
refers to the position of the selected item in the dataprovider. List#selectedIndex
是指所选项目在数据提供程序中的位置。 The first element will have index 0, the second index 1 and so on. 第一个元素的索引为0,第二个元素的索引为1,依此类推。 If no item is selected,
selectedIndex
will be -1
. 如果未选择任何项目,则
selectedIndex
将为-1
。
If you want to select or delete by firstName - as you do in your query - you will have to pass in a valid first name instead of an index position. 如果要按名字选择或删除-就像在查询中一样-您必须传递有效的名字而不是索引位置。 You can do this with the
List#selectedItem
property. 您可以使用
List#selectedItem
属性来执行此操作。 Also don't forget the single quotes in your query if you're not using query params. 如果您不使用查询参数,也不要忘记查询中的单引号。
"DELETE FROM UserTable " +
"WHERE firstName = '" + listBox.selectedItem.firstName + "'";
You weren't asking for this, but I'll tell you anyway: for security reasons you should use query params when using variables in your queries. 您并不是要这样做,但是无论如何我都会告诉您:出于安全原因,在查询中使用变量时应使用查询参数。 One way to achieve this in ActionScript is:
在ActionScript中实现此目的的一种方法是:
stmt.parameters[0] = listBox.selectedItem.firstName;
stmt.text = "DELETE FROM UserTable WHERE firstName = ?";
(no single quotes required here) (此处无需单引号)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.