Android第三方数据库框架litePal使用

1 代码参考自郭霖大神博客http://blog.csdn.net/guolin_blog/article/details/38556989

2 大神创造的框架一个字用起来就是

3 主界面MainActivity代码:

package com.example.androidlitepal;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

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.androidClass.Category;
import com.example.androidClass.Comment;
import com.example.androidClass.Introduction;
import com.example.androidClass.News;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends Activity implements OnClickListener {

	Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10, btn11, btn12, btn13, btn14, btn15, btn16, btn17,
			btn18, btn19, btn20, btn21, btn22, btn23, btn24, btn25, btn26, btn27, btn28, btn29, btn30, btn31, btn32;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn1 = (Button) findViewById(R.id.button1);
		btn2 = (Button) findViewById(R.id.button2);
		btn3 = (Button) findViewById(R.id.button3);
		btn4 = (Button) findViewById(R.id.button4);
		btn5 = (Button) findViewById(R.id.button5);
		btn6 = (Button) findViewById(R.id.button6);
		btn7 = (Button) findViewById(R.id.button7);
		btn8 = (Button) findViewById(R.id.button8);
		btn9 = (Button) findViewById(R.id.button9);
		btn10 = (Button) findViewById(R.id.button10);
		btn11 = (Button) findViewById(R.id.button11);
		btn12 = (Button) findViewById(R.id.button12);
		btn13 = (Button) findViewById(R.id.button13);
		btn14 = (Button) findViewById(R.id.button14);
		btn15 = (Button) findViewById(R.id.button15);
		btn16 = (Button) findViewById(R.id.button16);
		btn17 = (Button) findViewById(R.id.button17);
		btn18 = (Button) findViewById(R.id.button18);
		btn19 = (Button) findViewById(R.id.button19);
		btn20 = (Button) findViewById(R.id.button20);
		btn21 = (Button) findViewById(R.id.button21);
		btn22 = (Button) findViewById(R.id.button22);
		btn23 = (Button) findViewById(R.id.button23);
		btn24 = (Button) findViewById(R.id.button24);
		btn25 = (Button) findViewById(R.id.button25);
		btn26 = (Button) findViewById(R.id.button26);
		btn27 = (Button) findViewById(R.id.button27);
		btn28 = (Button) findViewById(R.id.button28);
		btn29 = (Button) findViewById(R.id.button29);
		btn30 = (Button) findViewById(R.id.button30);
		btn31 = (Button) findViewById(R.id.button31);
		btn32 = (Button) findViewById(R.id.button32);
		btn1.setOnClickListener(this);
		btn2.setOnClickListener(this);
		btn3.setOnClickListener(this);
		btn4.setOnClickListener(this);
		btn5.setOnClickListener(this);
		btn6.setOnClickListener(this);
		btn7.setOnClickListener(this);
		btn8.setOnClickListener(this);
		btn9.setOnClickListener(this);
		btn10.setOnClickListener(this);
		btn11.setOnClickListener(this);
		btn12.setOnClickListener(this);
		btn13.setOnClickListener(this);
		btn14.setOnClickListener(this);
		btn15.setOnClickListener(this);
		btn16.setOnClickListener(this);
		btn18.setOnClickListener(this);
		btn19.setOnClickListener(this);
		btn20.setOnClickListener(this);
		btn21.setOnClickListener(this);
		btn22.setOnClickListener(this);
		btn23.setOnClickListener(this);
		btn24.setOnClickListener(this);
		btn25.setOnClickListener(this);
		btn26.setOnClickListener(this);
		btn27.setOnClickListener(this);
		btn28.setOnClickListener(this);
		btn29.setOnClickListener(this);
		btn30.setOnClickListener(this);
		btn31.setOnClickListener(this);
		btn32.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			SQLiteDatabase db = Connector.getDatabase();
			Connector.getDatabase();
			db.close();
			break;
		case R.id.button2:
			News news = new News();
			news.setTitle("这是一条新闻标题");
			news.setContent("这是一条新闻内容");
			news.setPublishDate(new Date());
			// 使用saveThrows()方法来存储数据,一旦存储失败就会抛出一个DataSupportException异常,我们可以通过对这个异常进行捕获来处理存储失败的情况。
			// news.saveThrows();
			Log.d("TAG", "news id is " + news.getId());
			news.save();
			Log.d("TAG", "news id is " + news.getId());
			if (news.save()) {
				Toast.makeText(MainActivity.this, "存储成功", Toast.LENGTH_SHORT).show();
			} else {
				Toast.makeText(MainActivity.this, "存储失败", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.button3:
			Comment comment1 = new Comment();
			comment1.setContent("好评!");
			comment1.setPublishDate(new Date());
			comment1.save();
			Comment comment2 = new Comment();
			comment2.setContent("赞一个");
			comment2.setPublishDate(new Date());
			comment2.save();
			News news1 = new News();
			news1.getCommentList().add(comment1);
			news1.getCommentList().add(comment2);
			news1.setTitle("第二条新闻标题");
			news1.setContent("第二条新闻内容");
			news1.setPublishDate(new Date());
			news1.setCommentCount(news1.getCommentList().size());
			news1.save();
			break;
		case R.id.button4:
			Introduction tion = new Introduction();
			tion.setDigest("123456");
			tion.setGuide("456789");
			tion.save();
			News mews3 = new News();
			mews3.setTitle(tion.getDigest());
			mews3.setContent(tion.getGuide());
			mews3.setPublishDate(new Date());
			mews3.setIntroduction(tion);
			mews3.save();

			break;
		case R.id.button5:
			Category cat = new Category();
			cat.setName("1564");
			cat.save();
			Category cat1 = new Category();
			cat1.setName("1564q");
			cat1.save();
			List list = new ArrayList();
			list.add(cat);
			list.add(cat1);
			News news4 = new News();
			news4.setTitle(cat.getName());
			news4.setContent(cat1.getName());
			news4.setPublishDate(new Date());
			news4.setCategoryList(list);
			news4.save();
			break;
		case R.id.button6:
			List list_news = new ArrayList();
			News news11 = new News();
			news11.setTitle("news11");
			news11.setContent("news11");
			news11.setPublishDate(new Date());
			News news22 = new News();
			news22.setTitle("news22");
			news22.setContent("news22");
			news22.setPublishDate(new Date());
			News news33 = new News();
			news33.setTitle("news33");
			news33.setContent("news33");
			news33.setPublishDate(new Date());
			list_news.add(news11);
			list_news.add(news22);
			list_news.add(news33);
			DataSupport.saveAll(list_news);
			break;
		case R.id.button7:
			ContentValues values = new ContentValues();
			values.put("title", "今日iPhone6发布");
			DataSupport.update(News.class, values, 2);
			break;
		case R.id.button8:
			ContentValues values1 = new ContentValues();
			values1.put("title", "news111");
			DataSupport.updateAll(News.class, values1, "title = ?", "news11");
			break;
		case R.id.button9:
			ContentValues values2 = new ContentValues();
			values2.put("title", "今日IPhone100Plus发布");
			DataSupport.updateAll(News.class, values2, "title = ? and commentcount > ?", "第二条新闻标题", "0");
			break;
		case R.id.button10:
			ContentValues values3 = new ContentValues();
			values3.put("title", "今日iPhone6 Plus发布");
			DataSupport.updateAll(News.class, values3);
			break;
		case R.id.button11:
			News updateNews = new News();
			updateNews.setTitle("45456456");
			updateNews.update(2);
			break;
		case R.id.button12:
			News updateNews1 = new News();
			updateNews1.setTitle("45456456");
			updateNews1.updateAll("title = ? and commentcount > ?", "今日iPhone6 Plus发布", "0");
			break;
		case R.id.button13:
			News updateNews22 = new News();
			updateNews22.setToDefault("commentCount");
			updateNews22.updateAll();
			break;
		case R.id.button14:
			DataSupport.delete(News.class, 16);
			break;
		case R.id.button15:
			DataSupport.deleteAll(News.class, "title = ? and commentcount = ?", "45456456", "0");
			break;
		case R.id.button16:
			DataSupport.deleteAll(News.class);
			// 另外一种删除所有数据的办法,只要判断是否持久化,调用过save就是表示数据已经持久化,没有调用过就是这种方法不起作用
			// News news = new News();
			// news.delete();
			break;
		case R.id.button17:
			News newsnew = new News();
			newsnew.setTitle("这是一条新闻标题");
			newsnew.setContent("这是一条新闻内容");
			newsnew.setPublishDate(new Date());
			newsnew.save();
			News news18 = DataSupport.find(News.class, 18);
			Log.i("1", "" + news18.getId());
			Log.i("1", news18.getTitle());
			Log.i("1", news18.getContent());
			Log.i("1", "" + news18.getCommentCount());
			Log.i("1", "" + news18.getPublishDate());
			break;
		case R.id.button18:
			News firstNews = DataSupport.findFirst(News.class);
			Log.i("2", "" + firstNews.getId());
			Log.i("2", firstNews.getTitle());
			Log.i("2", firstNews.getContent());
			Log.i("2", "" + firstNews.getCommentCount());
			Log.i("2", "" + firstNews.getPublishDate());
			break;
		case R.id.button19:
			News lastNews = DataSupport.findLast(News.class);
			Log.i("3", "" + lastNews.getId());
			Log.i("3", lastNews.getTitle());
			Log.i("3", lastNews.getContent());
			Log.i("3", "" + lastNews.getCommentCount());
			Log.i("3", "" + lastNews.getPublishDate());
			break;
		case R.id.button20:
			News newsnew1 = new News();
			newsnew1.setTitle("这是一条新闻标题");
			newsnew1.setContent("这是一条新闻内容");
			newsnew1.setPublishDate(new Date());
			newsnew1.save();
			long[] ids = new long[] { 18, 19 };
			List newsList = DataSupport.findAll(News.class, ids);
			for (int i = 0; i < newsList.size(); i++) {
				Log.i("4",
						newsList.get(i).getId() + "--" + newsList.get(i).getTitle() + "--"
								+ newsList.get(i).getContent() + "--" + newsList.get(i).getCommentCount() + "--"
								+ newsList.get(i).getPublishDate());
			}
			break;
		case R.id.button21:
			List allNews = DataSupport.findAll(News.class);
			for (int i = 0; i < allNews.size(); i++) {
				Log.i("5",
						allNews.get(i).getId() + "--" + allNews.get(i).getTitle() + "--" + allNews.get(i).getContent()
								+ "--" + allNews.get(i).getCommentCount() + "--" + allNews.get(i).getPublishDate());
			}
			break;
		case R.id.button22:
			List newsList1 = DataSupport.where("commentcount = ?", "0").find(News.class);
			for (int i = 0; i < newsList1.size(); i++) {
				Log.i("6",
						newsList1.get(i).getId() + "--" + newsList1.get(i).getTitle() + "--"
								+ newsList1.get(i).getContent() + "--" + newsList1.get(i).getCommentCount() + "--"
								+ newsList1.get(i).getPublishDate());
			}

			break;
		case R.id.button23:
			List newsList2 = DataSupport.select("title", "content").where("commentcount = ?", "0")
					.find(News.class);
			for (int i = 0; i < newsList2.size(); i++) {
				Log.i("7", newsList2.get(i).getTitle() + "--" + newsList2.get(i).getContent());
			}
			break;
		case R.id.button24:
			List newsList3 = DataSupport.select("title", "publishdate").where("commentcount = ?", "0")
					.order("publishdate desc").find(News.class);
			for (int i = 0; i < newsList3.size(); i++) {
				Log.i("8", newsList3.get(i).getTitle() + "--" + newsList3.get(i).getPublishDate());
			}
			break;
		case R.id.button25:
			List newsList4 = DataSupport.select("title", "publishdate").where("commentcount = ?", "0")
					.order("publishdate desc").limit(2).find(News.class);
			for (int i = 0; i < newsList4.size(); i++) {
				Log.i("9", newsList4.get(i).getTitle() + "--" + newsList4.get(i).getPublishDate());
			}
			break;
		case R.id.button26:
			List newsList5 = DataSupport.select("title", "publishdate").where("commentcount = ?", "0")
					.order("publishdate desc").limit(1).offset(1).find(News.class);
			for (int i = 0; i < newsList5.size(); i++) {
				Log.i("10", newsList5.get(i).getTitle() + "--" + newsList5.get(i).getPublishDate());
			}
			break;
		case R.id.button27:
			Comment comment3 = new Comment();
			comment3.setContent("好评!");
			comment3.setPublishDate(new Date());
			comment3.save();
			Comment comment4 = new Comment();
			comment4.setContent("赞一个");
			comment4.setPublishDate(new Date());
			comment4.save();
			News news14 = new News();
			news14.getCommentList().add(comment3);
			news14.getCommentList().add(comment4);
			news14.setTitle("第二条新闻标题");
			news14.setContent("第二条新闻内容");
			news14.setPublishDate(new Date());
			news14.setCommentCount(news14.getCommentList().size());
			news14.save();

			/*
			 * 不知道你有没有发现,刚才我们所学的每一个类型的find()方法,都对应了一个带有isEager参数的方法重载,
			 * 这个参数相信大家一看就明白是什么意思了,设置成true就表示激进查询,这样就会把关联表中的数据一起查询出来了。
			 * 
			 * 但是这种查询方式LitePal并不推荐,因为如果一旦关联表中的数据很多,查询速度可能就会非常慢。
			 * 而且激进查询只能查询出指定表的关联表数据,但是没法继续迭代查询关联表的关联表数据。因此,这里我建议大家还是使用默认的懒加载更加合适
			 * ,至于如何查询出关联表中的数据,其实只需要在模型类中做一点小修改就可以了。 public class News extends
			 * DataSupport{ ...
			 * 
			 * public List getComments() { return DataSupport.where(
			 * "news_id = ?", String.valueOf(id)).find(Comment.class); }
			 * 
			 * } 可以看到,我们在News类中添加了一个getComments()方法,而这个方法的内部就是使用了一句连缀查询,
			 * 查出了当前这条新闻对应的所有评论。改成这种写法之后,我们就可以将关联表数据的查询延迟,当我们需要去获取新闻所对应的评论时,
			 * 再去调用News的getComments()方法,这时才会去查询关联数据。这种写法会比激进查询更加高效也更加合理。
			 */
			/*
			 * 原生查询 为即使使用了LitePal,你仍然可以使用原生的查询方式(SQL语句)来去查询数据。
			 * DataSuppport类中还提供了一个findBySQL()方法,使用这个方法就能通过原生的SQL语句方式来查询数据了,
			 * 如下所示:Cursor cursor = DataSupport.findBySQL(
			 * "select * from news where commentcount>?", "0");
			 * findBySQL()方法接收任意个字符串参数,其中第一个参数就是SQL语句,后面的参数都是用于替换SQL语句中的占位符的,
			 * 用法非常简单。另外,findBySQL()方法返回的是一个Cursor对象,这和原生SQL语句的用法返回的结果也是相同的。
			 */
			News newsrr = DataSupport.find(News.class, 20, true);
			Log.i("11", newsrr.getId() + "--" + newsrr.getTitle() + "--" + newsrr.getContent() + "--"
					+ newsrr.getCommentCount() + "--" + newsrr.getPublishDate());
			List commentList = newsrr.getCommentList();
			for (int i = 0; i < commentList.size(); i++) {
				Log.i("12", commentList.get(i).getContent() + "--" + commentList.get(i).getPublishDate());
			}
			break;
		case R.id.button28:
			int result = DataSupport.count(News.class);  
			Log.i("count", ""+result);
			break;
		case R.id.button29:
			int result1 = DataSupport.where("commentcount = ?", "0").count(News.class);
			Log.i("count", ""+result1);
			break;
		case R.id.button30:
			int result2 = DataSupport.sum(News.class, "commentcount", int.class);  
			Log.i("count", ""+result2);
			break;
		case R.id.button31:
			double result3 = DataSupport.average(News.class, "commentcount");
			Log.i("count", ""+result3);
			break;
		case R.id.button32:
			int result4 = DataSupport.max(News.class, "commentcount", int.class); 
			Log.i("count", ""+result4);
			break;
		default:
			break;
		}
	}
}
4 有需要参考的朋友直接下载代码就行不需要自己在敲一遍同时可以参考大神博客再看一遍这里我就不一一写都表示什么意思了,代码中也有注释

5代码下载地址:http://download.csdn.net/detail/qq_31546677/9805322

你可能感兴趣的:(Android第三方数据库框架litePal使用)