Sqlite之删除超过N条的数据

历史记录,搜索历史记录等,这些记录又不能无限的存储,只需要记录最新的N条就可以。

bean对象

import com.lidroid.xutils.db.annotation.Column;
import com.lidroid.xutils.db.annotation.Id;
import com.lidroid.xutils.db.annotation.NoAutoIncrement;
import com.lidroid.xutils.db.annotation.Table;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.java.Log;

/**
 * Created by xdjaxa on 2017/6/20.
 */
@Table(name = "sandbox_search_table")
@Data
@Log
@AllArgsConstructor
@NoArgsConstructor
public class SanBoxSerach implements Serializable{
    @Id
    @NoAutoIncrement
    @Column(column = "_id")
    private String id;

    @Column(column = "pid")
    @Getter
    @Setter
    private String pid;

    @Column(column = "time")
    @Getter
    @Setter
    private long time;
}

sql语句

String sql = "delete from sandbox_search_table where" +
                        " (select count(time) from sandbox_search_table )> 9 and " +
                        " time in (select time from sandbox_search_table order by time desc limit " +
                        " (select count(time) from sandbox_search_table) offset 9 )";

参考:
http://blog.sina.com.cn/s/blog_89f6a9130101hvv3.html

    原文作者:OnlyRose
    原文地址: https://www.jianshu.com/p/3ca800ac08eb
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞