LitePal简介
LitePal是一款开源的Android数据库框架,采用对象关系映射(ORM)模式,将常用的数据库功能进行封装,可以不用写一行SQL语句就可以完成创建表、增删改查的操作。并且很轻量级,jar包不到100k,几乎零配置。
使用步骤如下
android studio中可以直接添加依赖:
// litepal数据库
compile ‘org.litepal.android:core:1.6.1’
下面的操作eclipse和android studio都是一样的了。
eclipse中在项目中新建libs文件夹,把jar包行进去:
新建assets目录,然后创建 litepal.xml资源文件,用于创建数据库名称、数据库版本、表名、数据库存放的位置;
<?xml version="1.0" encoding="UTF-8"?>页面activity_main.xml,就定义了几个按钮,用于测试
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:id="@+id/LinearLayout1"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=" r e l a t i v e P a c k a g e . {relativePackage}. relativePackage.{activityClass}" >
<Button
android:id="@+id/button1"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“创建数据库” />
<Button
android:id="@+id/button2"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“添加数据” />
<Button
android:id="@+id/button3"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“删除” />
<Button
android:id="@+id/button4"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“修改” />
<Button
android:id="@+id/button5"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“查找” />
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(Integ
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
er 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{