您的位置:首页 > 数据库

LitePal专业数据库框架使用

2015-06-11 10:40 225 查看
package com.example.litepaldemo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;
import com.example.litepaldemo.bean.Category;
import com.example.litepaldemo.bean.Comment;
import com.example.litepaldemo.bean.Introduction;
import com.example.litepaldemo.bean.News;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//LitePal数据库框架使用Demo
public class MainActivity extends Activity implements OnClickListener{
private Button btnAdd,btnDelete,btnChange,btnCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnChange.setOnClickListener(this);
btnCheck.setOnClickListener(this);
SQLiteDatabase db=Connector.getDatabase();
}
private void initView() {
// TODO Auto-generated method stub
btnAdd=(Button) findViewById(R.id.btnAdd);
btnDelete=(Button) findViewById(R.id.btnDelete);
btnChange=(Button) findViewById(R.id.btnChange);
btnCheck=(Button) findViewById(R.id.btnCheck);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnAdd://增加
//一:普通存储
for (int i = 0; i < 10; i++) {
News news = new News();
news.setTitle("第"+i+"条新闻");
news.setContent("内容"+i);
news.setPublishDate(new Date());
boolean ifSaveSuccess=news.save();
if(ifSaveSuccess)
{
Toast.makeText(MainActivity.this, "存储成功!", 0).show();
}
}

//二:存储整个集合
List<News> newsList=new ArrayList<News>();
for (int i = 0; i < 10; i++) {
News news = new News();
news.setTitle("第"+i+"条新闻");
news.setContent("内容"+i);
news.setPublishDate(new Date());
newsList.add(news);
}
DataSupport.saveAll(newsList);

//三:具有表关联的存储
Comment comment1=new Comment();
comment1.setContent("评论1");
comment1.setPublishdate(new Date());
comment1.save();

Comment comment2=new Comment();
comment2.setContent("评论2");
comment2.setPublishdate(new Date());
comment2.save();

News news=new News();
news.getCommentList().add(comment1);
news.getCommentList().add(comment2);
news.setTitle("头条新闻");
news.setContent("马云批评京东...");
news.setPublishDate(new Date());
news.setCommentCount(news.getCommentList().size());
news.save();

//如果想要在存储失败的情况下抛出异常,就用saveThrows()来存储数据
/*News news0 = new News();
news.setTitle("这是一条新闻标题");
news.setContent("这是一条新闻内容");
news.setPublishDate(new Date());
try {
news.saveThrows();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/

break;
case R.id.btnDelete://删除
//1.删除单条数据(根据表名和id来删除) 这个方法会返回删除的数据条数
//注意:如果删除的数据所在的表与其他的表建立了关联,那么会删除与之关联的表中关联此条数据的数据
//例如:id=1 的新闻被删除了 而这条新闻对应的是 id=1,2,3的评论,那么这些评论也会被删除
//DataSupport.delete(News.class, 2);

//2.根据条件删除数据
//DataSupport.deleteAll(News.class, "title=? and commentcount=?","Iphone7今日发布!","0");

//3.删除特定表中的所有数据
//DataSupport.deleteAll(News.class);

//4.对象可以通过delete()方法自行删除,如果对象没有进行持久化(也就是保存在数据库中)
//例如News news=new News();
//news.delete();
//这样不会删除任何数据
//但是如果对象已经持久化了,会删除此条数据
//News news=new News(); news.save(); news.delete();
/*News news=new News();
if(news.isSaved())//判断对象是否已经持久化
{
news.delete();
}*/
break;
case R.id.btnChange://修改
//一:使用ContentValus修改 这个稍微复杂一点

//1.修改某个id的某个字段
/*ContentValues contentValues1=new ContentValues();
contentValues1.put("title","Iphone6今日发布!");
//三个参数:1.bean(哪张表)  2.值(包含修改的字段以及修改后的值) 3.id
DataSupport.update(News.class,  contentValues1,           10);*/

/**2.根据条件修改
* 注意:updateAll方法中?为占位符,表示条件个数,有多少个"?"后面就要有多少个条件
* */
//1.单个约束条件的修改
/*ContentValues contentValues2=new ContentValues();
contentValues2.put("title","Iphone6 Plus今日发布!");
DataSupport.updateAll(News.class, contentValues2, "title=?","Iphone6今日发布!");*/

//2.多个约束条件的修改
/*ContentValues contentValues3=new ContentValues();
contentValues3.put("title", "Iphone6 Plus今日发布!");
DataSupport.updateAll(News.class, contentValues3, "title=? and commentCount>?",
"Iphone6今日发布!",
"0");*/

//3:修改整张表的数据:只要将约束条件去掉就行了
/*ContentValues contentValues4=new ContentValues();
contentValues4.put("title", "Iphone7今日发布!");
DataSupport.updateAll(News.class, contentValues4);*/

//二:使用LitePal 自定义的修改方式来修改
//1.修改某个id的数据的某个字段
/*News updataNews1=new News();
updataNews1.setTitle("修改了!");
updataNews1.update(18);*/

//2.根据条件修改
/*News updataNews2=new News();
updataNews2.setTitle("今日Iphone10发布");
updataNews2.updateAll("title=? and id>?","Iphone7今日发布!","11");*/

//3.将 某一字段的数据恢复默认值
/*News updataNews3=new News();
updataNews3.setToDefault("commentCount");
updataNews3.updateAll();
break;*/

case R.id.btnCheck://查询
//1.指定表名与id查询单条数据
/*News news=DataSupport.find(News.class,5);
Toast.makeText(MainActivity.this, news.getPublishDate().toString(), 0).show();*/

//2.查询表中的首条与尾条数据
/*News newsF=DataSupport.findFirst(News.class);//查询首条数据
Toast.makeText(MainActivity.this, newsF.getId()+"", 0).show();
News newsL=DataSupport.findLast(News.class);//查询尾条数据
Toast.makeText(MainActivity.this, newsL.getId()+"", 0).show();*/

//3.指定多个id查询数据

/*3.1自定义多个id查询
* List<News> myNewsList=DataSupport.findAll(News.class,1,3,5,7,9);
Toast.makeText(MainActivity.this, myNewsList.size()+"", 0).show();*/

/**3.2使用id数组查询:注意ids为Long类型的数组
* */
/*long[] ids=new long[]{2,4,6,8,10};
List<News> myNewsList=DataSupport.findAll(News.class,ids);
Toast.makeText(MainActivity.this, myNewsList.size()+"", 0).show();*/

//4.查询所有数据
/*List<News> newsList2=DataSupport.findAll(News.class);
Toast.makeText(MainActivity.this, newsList2.size()+"", 0).show();*/

//5.根据其他条件查询数据
//5.1 单条件查询
/*List<News> newsList3=DataSupport.where("commentCount>?","0").find(News.class);
Toast.makeText(MainActivity.this, newsList3.size()+"", 0).show();*/
//5.2 多条件查询
/*List<News> newsList4=DataSupport.where("commentCount=? and id>?","0","5").find(News.class);
Toast.makeText(MainActivity.this, newsList4.size()+"", 0).show();*/

/**6.根据条件查询数据以及查询指定列(一列或几列的数据)
* 你可能并不需要那么多列的数据
* LitePal支持查询自定义列的数据,如果你用get方法获取其他列的数据,你会得到null
* */
/*List<News> newsList5=DataSupport.select("id","content").where("id>?","5").find(News.class);
Toast.makeText(MainActivity.this, newsList5.get(0).getContent(), 0).show();*/

/**
* 7.查询结果排序
* 你可以把查询出的结果按照一个可以比较的字段正序或者倒叙排列,
* 比如说,我希望将查询出的新闻按照发布的时间倒序排列,
* 即最新发布的新闻放在最前面,那就可以这样写
* asc表示正序 desc表示倒序*/
//7.1 排序查找
/*List<News> newsList6=DataSupport.select("id").order("id desc").find(News.class);
Toast.makeText(MainActivity.this, newsList6.get(0).getId()+"", 0).show();*/

//7.2 限制结果条数的查找
/*List<News> newsList7=DataSupport.select("id").order("id desc").limit(5).find(News.class);
Toast.makeText(MainActivity.this, newsList7.get(4).getId()+"", 0).show();*/

/**8.设置偏移量的查询
* 刚才我们查询到的是所有匹配条件的前5条新闻,
* 那么现在我想对新闻进行分页展示,
* 翻到第二页时,
* 展示第6到第10条新闻
* */
/*List<News> newsList8=DataSupport.select("id").order("id desc").limit(5).offset(5).find(News.class);
Toast.makeText(MainActivity.this, newsList8.get(0).getId()+"", 0).show();*/

/**9.激进查询
* 以上查询都只能查询到某一张表的数据,如果我们需要查询关联表的数据,比如通过新闻查到评论数据,
* 就需要使用激进查询了
* */
//9.1比如我们要查询id为1的新闻对应的所有评论,可以这样查
/*News news9=DataSupport.find(News.class, 1, true);
List<Comment> comments=news9.getCommentList();*/

/**
* 这样查询以后commentsList中的数据也会被查询出来
* 但是这样查询有个弊端,一旦关联表中的数据很多,查询速度会非常慢,不推荐这样查询
* 我们可以在模型类中作一点小小的修改,将getCommentList()方法的返回值改为查询数据库的语句
* 那么我们在查询News表的数据时,就会预查询评论表的数据,这样更加高效*/
//9.2预查询
/*News news9=DataSupport.find(News.class, 1,true);
List<Comment> comments=news9.getCommentList();
Toast.makeText(MainActivity.this,comments.get(0).getContent(), 0).show();*/

/**10.统计查询
* LitePalcount()提供了
* sum()、
* average()、
* max()
* 和min()这五种聚合函数
* */

//10.1:统计一张表的行数
//统计表的行数,不带条件
/*int count1=DataSupport.count(News.class);
Toast.makeText(MainActivity.this,count1+"", 0).show();
//统计表的行数,带条件
int count2=DataSupport.where("id>?","5").count(News.class);
Toast.makeText(MainActivity.this,count2+"", 0).show();*/

//10.2:统计关联表的数据总数
//例如,统计新闻表中评论的总条数
//三个参数                                                1.表的类名        2.统计的字段名           3.返回值的类型
//int count3=DataSupport.sum(News.class,"commentCount", int.class);

//10.3: 统计平均数
//比如我们要统计平均每条消息,由于平局数一般有小数,所以平局数为double类型
double count4=DataSupport.average(News.class,"commentCount");

//10.4: 求最值
//1.最大值
//比如我们想要知道news表中所有新闻里面最高的评论数是多少,就可以这样写
/*int max=DataSupport.max(News.class,"commentCount", int.class);

//2.最小值
int min=DataSupport.max(News.class,"commentCount", int.class);*/
break;
default:
break;
}
}

}
转自:http://www.apkbus.com/android-227228-1-1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: