轉發郭神的blog,講的非常詳細,是基於1.6版本,但現在使用的是2.0,有點差別
https://blog.csdn.net/guolin_blog/article/details/38461239
1.首先說一下常用查看數據庫adb命令
手機root后 adb shell ->cd data/data/
ls 展示列表 cd進入應用 cd databases進入數據庫
sqlite3 demo.db注意選擇有.db文件
.table 展示數據庫表格
.mode line 列表形式展示數據
pragma table_info(表名); 注意要有分號 查看表格列信息
select * from 表名; 查看添加內容
ctrl+d 退出sqlite
2.配置
(1)在app module dependencies導入
implementation 'org.litepal.android:java:3.0.0'
(2)在src->main->assets 添加litepal.xml。一定要新建文件+寫擴展名的形式,不要直接新建xml
<?xml version="1.0" encoding="utf-8" ?>
<litepal>
<dbname value="demo" ></dbname>
<version value="6" ></version>
<list>
<mapping class="com.tayh.litepaldemo.News"></mapping>
<mapping class="com.tayh.litepaldemo.Comment"></mapping>
<mapping class="com.tayh.litepaldemo.Introduction"></mapping>
<mapping class="com.tayh.litepaldemo.Category"></mapping>
</list>
</litepal>
(3)MyApplication 可以直接繼承Application ,初始化LitePal就可以了
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
}
(4)Activity 調用數據庫語句
SQLiteDatabase db = Connector.getDatabase();
(5)News 與Introduction 一對一 外鍵news_id生成在Introduction里,News本身並沒有key,這個需要理解一下。News與Category 多對多,生成key在新生成的category_news表中
public class News extends LitePalSupport {
private int id;
private String title;
private String content;
private Date publishDate;
private int commentCount;
private Introduction introduction;//一對一 外鍵生成在Introduction表里
private List<Comment> commentList = new ArrayList<Comment>();//一對多
private List<Category> categoryList = new ArrayList<Category>();//多對多
}
public class Introduction extends LitePalSupport {
private int id;
private String guide;
private String digest;
}
public class Comment extends LitePalSupport {
private int id;
private String content;
private Date publishDate;
private News news;//多對一
}
public class Category extends LitePalSupport {
private int id;
private String name;
private List<News> newsList = new ArrayList<News>();//多對多
}
3.增刪改查CRUD
(1)存儲
* save()方法用於數據儲存,返回值為是否存儲成功
saveThrows() 存儲失敗拋出異常
news.getId() 可以獲取到儲存id
Comment comment1 = new Comment();
comment1.setContent("comment1");
comment1.setPublishDate(new Date());
comment1.save();
News news = new News();
news.setTitle("news1");
news.setContent("content1");
news.setPublishDate(new Date());
news.getCommentList().add(comment1);
news.setCommentCount(news.getCommentList().size());
news.save();
*也可存儲集合
List<News> newsList;
...
LitePal.saveAll(newsList);
(2)更新
有兩種方式:
方式一:使用ContentValue
ContentValues values = new ContentValues();
values.put("title","test1");//title 列名
LitePal.update(News.class,values,1);//1 是更新列的id
//LitePal.update(News.class,values);//更新所有
條件更新:
ContentValues values = new ContentValues();
values.put("title","test2");
LitePal.updateAll(News.class, values, "title = ? and commentcount > ?", "test1", "0");
方法二:數據直接更新
News updateNews = new News();
updateNews.setTitle("test0");
updateNews.update(1);//id
條件更新:
News updateNews = new News();
updateNews.setTitle("test1");
updateNews.updateAll("title = ? and commentcount > ?", "test0", "0");
注意:如果要恢復默認值使用setToDefault ,set 0無效
News updateNews = new News();
updateNews.setToDefault("commentCount");//commentCount列恢復默認值
updateNews.updateAll();
(3)刪除
刪除數據的同時,會把該列id作為外鍵的關聯表的數據一起刪除
int deleteCount = LitePal.delete(News.class, 1);//id 1
// LitePal.deleteAll(News.class, "title = ? and commentcount = ?", "test1", "0");
判斷持久化方法,即是否存到數據庫
if (news.isSaved()) {
news.delete();
}
(4)查詢
*查詢第一個或最后一個
News firstNews = LitePal.findFirst(News.class);
News lastNews = LitePal.findLast(News.class);
*按照多個id查詢
List<News> newsList = LitePal.findAll(News.class, 0, 2);
//方法二
long[] ids = new long[] { 0, 1};
List<News> newsList2 = LitePal.findAll(News.class, ids);
*按照條件查詢
//查詢news表,條件為commentcount >0 ,只要 title content列的數據 ,降序 ,limit(10) 前10條 ,offset(10)偏移量10 即11-20條數據
List<News> newsList3 = LitePal.select("title", "content")
.where("commentcount > ?", "0")
.order("publishdate desc").limit(10).offset(10)
.find(News.class);
激進查詢
方法一:設置為true,查出news關聯表的數據,不推薦,比較慢
News news = LitePal.find(News.class, 1, true);
List<Comment> commentList = news.getCommentList();
方法二:News表增加getComments()方法。然后需要時再獲取list
public class News extends LitePalSupport{
...
public List<Comment> getComments() {
return LitePal.where("news_id = ?", String.valueOf(id)).find(Comment.class);
}
}
News news2 = LitePal.find(News.class, 1);
List<Comment> commentList2 = news2.getComments();
*原生查詢
Cursor cursor = LitePal.findBySQL("select * from news where commentcount>?", "0");
- 1
4.聚合函數
LitePal中一共提供了count()、sum()、average()、max()和min()這五種聚合函數
//統計行數
int result = LitePal.count(News.class);
// int result1 = LitePal.where("commentcount = ?", "0").count(News.class);
//結果求和
int result2 = LitePal.sum(News.class, "commentcount", int.class);
//結果求平均
double result3 = LitePal.average(News.class, "commentcount");
//求最大值
int result4 = LitePal.max(News.class, "commentcount", int.class);
//求最小值
int result5 = LitePal.min(News.class, "commentcount", int.class);