Android——LitePal第三方数据库框架详解

在学Litepal之前如果没学过数据库,可以先了解一下SQLite,地址:https://blog.csdn.net/qq_40205116/article/details/88739592

LitePal简介

LitePal是一款开源的Android数据库框架,采用对象关系映射(ORM)模式,将常用的数据库功能进行封装,可以不用写一行SQL语句就可以完成创建表、增删改查的操作。并且很轻量级,jar包不到100k,几乎零配置。

使用步骤如下

android studio中可以直接添加依赖:

// litepal数据库
compile 'org.litepal.android:core:1.6.1'

下面的操作eclipse和android studio都是一样的了。

eclipse中在项目中新建libs文件夹,把jar包行进去:

Android——LitePal第三方数据库框架详解_第1张图片

新建assets目录,然后创建 litepal.xml资源文件,用于创建数据库名称、数据库版本、表名、数据库存放的位置;



    
    
    
    
    
    
        
    

页面activity_main.xml,就定义了几个按钮,用于测试

Android——LitePal第三方数据库框架详解_第2张图片



    

TbUser.java定义的实体类,要继承DataSupport 

package com.mcy.litepaldemo.model;

import java.util.Date;

import org.litepal.crud.DataSupport;

public class TbUser extends DataSupport {
	private Integer id;
	private String username;
	private String password;
	private String name;
	private String sex;
	private Date born;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Date getBorn() {
		return born;
	}
	public void setBorn(Date born) {
		this.born = born;
	}
	public TbUser(String username, String password, String name, String sex,
			Date born) {
		super();
		this.username = username;
		this.password = password;
		this.name = name;
		this.sex = sex;
		this.born = born;
	}
	public TbUser(Integer id, String username, String password, String name,
			String sex, Date born) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.name = name;
		this.sex = sex;
		this.born = born;
	}
	public TbUser() {
		super();
	}
}

MainActivity.java增删改查实际操作:

package com.mcy.litepaldemo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import com.mcy.litepaldemo.model.TbUser;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        button5 = (Button) findViewById(R.id.button5);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        button5.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		TbUser tbUser = new TbUser();
		switch (v.getId()) {
		case R.id.button1:
			//调用LitePal插件中getDAtabase方法创建数据库
			LitePal.getDatabase();
			break;
		case R.id.button2:	//增
			SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd");
			tbUser.setName("张三");
			tbUser.setUsername("admin");
			tbUser.setPassword("admin");
			tbUser.setSex("男");
			try {
				tbUser.setBorn(ft.parse("2019-03-06"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			tbUser.save();
			break;
		case R.id.button3:	//删
			//删除TbUser类中id=3的元素
			DataSupport.delete(TbUser.class, 3);
			//根据条件删除
			//DataSupport.deleteAll(TbUser.class, "sex = ?", "女");
			break;
		case R.id.button4:	//改
			tbUser.setSex("女");
			tbUser.setUsername("abc");
			//修改
			tbUser.updateAll("id = ? or id = ?", "2", "4");
			break;
		case R.id.button5:	//查
			//查找id=2的元素
			//TbUser user = DataSupport.find(TbUser.class, 2);
			//查找数据库表中第一条数据
			//TbUser user = DataSupport.findFirst(TbUser.class);
			//查找数据库中第二条数据
			//TbUser user = DataSupport.findLast(TbUser.class);
			//查找id等于1和3的数据
			List list = DataSupport.findAll(TbUser.class, 1, 6);
			//按条件查找
			/*DataSupport.select("id", "username")
			.where("sex=?", "男")
			.order("username desc")
			.find(TbUser.class);*/
			
			//自己定义SQL语句查询
			//DataSupport.findBySQL(sql);
			break;
		default:
			break;
		}
	}
}

 

你可能感兴趣的:(Android)