LitePal操作数据库
LitePal是一款开源的Android数据库框架,它采用对象关系映射(ORM)模式。
一.配置LitePal
1.LitePal的引入
打开app目录下的build.gradle文件,在dependencies闭包中添加依赖“implementation 'org.litepal.guolindev:core:3.2.3'”
dependencies { ... implementation 'org.litepal.guolindev:core:3.2.3' }
其中3.2.3为版本号,可以在LitePal项目主页查看最新版本号。最后点击右上角的"Sync New"同步更新,项目引入LitePal成功。
2.litepal.xml文件配置
1.在app/src/main目录下新建一个目录文件assets,右击main->New->Directory,输入assets,点击"OK"
img_1.png
2.在新建的assets文件下,自定义一个litepal.xml文件,右击assets->New->XML->Values XML File,输入litepal,点击“OK”
img_2.png
3.编辑litepal.xml文件
<?xml version="1.0" encoding="utf-8"?><litepal> <dbname value="UserDatabase"></dbname> <version value= "1"></version> <list></list></litepal>
其中<dbname>用于指定数据名称,例如这里定义的数据库名称为UserDatabase ;<version>用于指定版本号;<list>用于指定映射模型。
4.打开AndroidManifest.xml文件,在<application></application>里配置LitePalApplication
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxx.xxx"> <application android:name="org.litepal.LitePalApplication" ... </application></manifest>
二.数据库操作
1.创建数据库
1.定义一个User类,声明属性字段,并生成对应的getter和setter方法。这里的User类就对应数据库中的User表,而User类中的每个字段就分别对应User表中的每一个列。例如:
public class User { //声明 private int id; private String name; private double height; private int age;}
这里的User 对应User 表,id、name、height、age分别对应User表中的id列、name列、height列、age列。
2.在litepal.xml文件中,使用<mapping>标签声明需要配置的映射模型类User
<list> <mapping class="com.xxx.xxx.User"></mapping></list>
3.点击按钮创建数据库
//创建数据库Button createBtn = findViewById(R.id.create_data);createBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Connector.getDatabase(); }});
getDatabase()这个方法是getWritableDatabase的别名,直接调用 getDatabase 会默认调用 getWritableDatabase 方法。
2.升级数据库
1.在User表中新增字段:在User类中声明属性字段,然后在litepal.xml里版本号加1
2.新增表Category表:和User类一样,创建Category类,声明属性字段,然后在litepal.xml中使用<mapping>标签声明需要配置的映射模型类Category,以及版本号加1
<version value= "2"></version><list> <mapping class="com.example.litepaldatabase.User"></mapping> <mapping class="com.example.litepaldatabase.Category"></mapping> </list>
3.CRUD
对数据库进行CRUD操作时,必须要继承LitePalSupport类
public class User extends LitePalSupport { ...}
1.添加(Create)
//插入第一条数据User user = new User();user.setId(1);user.setName("浅若清风");user.setHeight(165.5);user.setAge(31);user.save();//插入第二条数据User user1 = new User();user1.setId(2);user1.setName("浅若清风_1");user1.setHeight(166);user1.setAge(32);user1.save();
调用setName()、setHeight()、setAge()分别来设置名称、身高、年龄,最后调用save()方法来存储模型。
2.更新(Update)
//更新数据User user = new User();user.setHeight(166);user.setAge(18);user.updateAll("id = ? and name = ?","1","浅若清风");
调用setHeight()、setAge()来分别设置新的身高和年龄,最后调用updateAll()来指定一个条件约束,如果不指定,则表示更新所有数据。我们知道,Java中所有的数据类型都有默认值,如果想把数据设置成默认值,使用setToDefault()方法。例如把id为1、name为“浅若清风”的age设置成默认值
User user = new User();user.setToDefault("age");user.updateAll("id = ? name = ?","1","浅若清风");
5.删除(Delete)
1.LitePal 中的静态删除方法
//delete()方法删除指定id的单个记录LitePal.delete(User.class,1);
第一个参数用于指定哪张表的数据,第二个参数用于指定删除该表中的哪一条数据,例如这里这里的传1就表示删除User表中的id为1的数据。
//deleteAll()方法删除多个数据LitePal.deleteAll(User.class,"age > ?","30");
第一个参数用于指定哪张表的数据,第二和第三个参数用于约束删除哪些行的数据,例如这里就表示删除User表中的age大于30的数据。
2.调用已存储数据的delete()方法
User user = new User();user.delete();
6.查询(Retrieve)
1.id查询
//查询指定id单个记录User user1 = LitePal.find(User.class,1);//查询所有记录List<User> user2 = LitePal.findAll(User.class);//查询第一数据User user3 = LitePal.findFirst(User.class);//查询最后一条数据User user4 = LitePal.findLast(User.class);
2.连缀查询
//select()用于指定查询哪些列的数据List<User> user5 = LitePal.select("name","age").find(User.class);//where()用于指定查询的约束条件List<User> user6 = LitePal.where("age > ? ","30").find(User.class);//order()用于指定查询结果的排序,desc表示降序,asc或者不写表示升序List< User> user7 = LitePal.order("age desc").find(User.class);//limit()用于查询结果的数量List<User> user8 = LitePal.limit(2).find(User.class);//offset()用于指定查询结果的偏移量List< User > user9 = LitePal.limit(2).offset(1).find(User.class);
3.激进查询
User user10 = LitePal.findFirst(User.class,true);User user11 = LitePal.findLast(User.class,true);
完整示例代码
//build.gradle文件apply plugin: 'com.android.application'android { ...}dependencies { ... //添加LitePal依赖 implementation 'org.litepal.guolindev:core:3.2.3'}
<!--AndroidManifest.xml文件--><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxx.xxx"> <application android:name="org.litepal.LitePalApplication" ... </application></manifest>
<!--litepal.xml文件--><?xml version="1.0" encoding="utf-8"?><litepal> <dbname value="UserDatabase"></dbname> <version value= "1"></version> <list> <mapping class="com.example.litepaldatabase.User"></mapping> <!--新增Category表时,version value= "2"或更多 <mapping class="com.example.litepaldatabase.Category"></mapping> --> </list>
//User类public class User extends LitePalSupport { //声明 private int id;//id private String name;//名称 private double height;//身高 private int age;//年龄 public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}
//.java文件public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建数据库 Button createBtn = findViewById(R.id.create_data); createBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Connector.getDatabase(); } }); //添加数据 Button addBtn = findViewById(R.id.add_data); addBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //插入第一条数据 User user = new User(); user.setId(1); user.setName("浅若清风"); user.setHeight(165.5); user.setAge(31); user.save(); //插入第二条数据 User user1 = new User(); user1.setId(2); user1.setName("浅若清风_1"); user1.setHeight(166); user1.setAge(32); user1.save(); } }); //更新数据 Button updateBtn = findViewById(R.id.update_data); updateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { User user = new User(); user.setHeight(167); //age设置成默认值 user.setToDefault("age"); user.updateAll("id = ? and name = ?","1","浅若清风"); } }); //删除数据 Button deleteBtn = findViewById(R.id.delete_data); deleteBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** * delete()方法删除指定id的单个记录 **/ LitePal.delete(User.class,1); /** * deleteAll()方法删除满足约束条件多个记录 * LitePal.deleteAll(User.class,"age > ?","30"); **/ /** * 删除表的所有数据 * User user = new User(); * user.delete(); **/ } }); //查询数据 Button retrieveBtn = findViewById(R.id.retrieve_data); retrieveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /** * id查询 **/ //查询指定id单个记录 User user1 = LitePal.find(User.class,1); //查询所有记录 List< User > user2 = LitePal.findAll(User.class); //查询第一数据 User user3 = LitePal.findFirst(User.class); //查询最后一条数据 User user4 = LitePal.findLast(User.class); /** * 连缀查询 **/ //select()用于指定查询哪些列的数据 List< User > user5 = LitePal.select("name","age").find(User.class); //where()用于指定查询的约束条件 List< User > user6 = LitePal.where("age > ? ","30").find(User.class); //order()用于指定查询结果的排序,desc表示降序,asc或者不写表示升序 List< User > user7 = LitePal.order("age desc").find(User.class); //limit()用于查询结果的数量 List< User > user8 = LitePal.limit(1).find(User.class); //offset()用于指定查询结果的偏移量 List< User > user9 = LitePal.limit(1).offset(1).find(User.class); /** * 激进查询 **/ User user10 = LitePal.findFirst(User.class,true); User user11 = LitePal.findLast(User.class,true); /** * 解析 **/ //单条数据 int id = user1.getId(); String name = user1.getName(); Double height = user1.getHeight(); int age = user1.getAge(); //List数据 for (User user:user2) { int id2 = user.getId(); String name2 = user.getName(); Double height2 = user.getHeight(); int age2 = user.getAge(); } } }); }}
<!--xml布局--><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--创建数据库按钮--> <Button android:id="@+id/create_data" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Create" /> <!--添加数据按钮--> <Button android:id="@+id/add_data" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Add" /> <!--更新数据按钮--> <Button android:id="@+id/update_data" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Update" /> <!--删除数据按钮--> <Button android:id="@+id/delete_data" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Delete" /> <!--查询数据按钮--> <Button android:id="@+id/retrieve_data" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="Retrieve" /></LinearLayout>
作者:浅_若清风
链接:https://www.jianshu.com/p/444f59cf4882
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。