Updating Existing Records—Another Use for WHERE(更新已有的记录——WHERE 再战江湖)

    有了完美通用的 selectwhere 函数,是时候开始编写下一个所有数据库都需要的特性——更新特定记录的方法了。在 SQL 中,update 命令被用于更新一组匹配特定 where 子句的记录。这听起来像是个很好的模型,尤其是当已经有了一个 where 子句生成器时。事实上,update 函数只是你已经见过的一些思路的再次应用:使用一个通过参数传递的选择器函数来选取需要更新的记录,再使用关键字形参来指定需要改变的值。这里主要出现的新内容是对 MAPCAR 函数的使用,其映射在一个列表上(这里是 ),然后返回一个新的列表,其中含有在原来列表的每个 元素上调用一个函数所得到的结果。

    One other new bit here is the use of SETF on a complex form such as (getf row :title). I’ll discuss SETF in greater detail in Chapter 6, but for now you just need to know that it’s a general assignment operator that can be used to assign lots of “places” other than just variables. (It’s a coincidence that SETF and GETF have such similar names—they don’t have any special relationship.) For now it’s enough to know that after (setf (getf row :title) title), the plist referenced by row will have the value of the variable title following the property name :title. With this update function if you decide that you really dig the Dixie Chicks and that all their albums should go to 11, you can evaluate the following form:

    And it is so.

    这样就可以了。

    甚至可以更容易地添加一个函数来从数据库里删除行。

    The function REMOVE-IF is the complement of REMOVE-IF-NOT; it returns a list with all the elements that do match the predicate removed. Like REMOVE-IF-NOT, it doesn’t actually affect the list it’s passed but by saving the result back into *db*, delete-rows actually changes the contents of the database.