android LitePal数据库简单操作操作

  1. Android Studio,需要在build.gradle中修改一下配置即可:
    dependencies {
        implementation 'org.litepal.android:core:2.0.0'
    }

    最新版本号可到官网查阅:https://github.com/LitePalFramework/LitePal

  2. 配置litepal.xml文件,创建一个assets文件夹,如果存在可直接再assets中直接操作(app-->src-->main目录-->New–Directory),然后再该文件夹中新建litepal.xml,如下 图  
<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <!--
		Define the database name of your application. 
		By default each database name should be end with .db. 
		If you didn't name your database end with .db, 
		LitePal would plus the suffix automatically for you.
		For example:    
    	<dbname value="demo" />
    -->
    <dbname value="sample" />  
    <!--
    	Define the version of your database. Each time you want 
    	to upgrade your database, the version tag would helps.
    	Modify the models you defined in the mapping tag, and just 
    	make the version value plus one, the upgrade of database
    	will be processed automatically without concern.
		For example:    
    	<version value="1" />
    -->
    <version value="2" />

    <!--
    	Define your models in the list with mapping tag, LitePal will
    	create tables for each mapping class. The supported fields
    	defined in models will be mapped into columns.
    	For example:    
    	<list>
    		<mapping class="com.test.model.Reader" />
    		<mapping class="com.test.model.Magazine" />
    	</list>
    -->
    <list>
        <mapping class="org.litepal.litepalsample.model.Album" />
        <mapping class="org.litepal.litepalsample.model.Song" />
        <mapping class="org.litepal.litepalsample.model.Singer" />
       
    </list>

    <!--
    	Define the cases of the tables and columns name. Java is a
    	case sensitive language, while database is case insensitive.
    	LitePal will turn all classes names and fields names into lowercase
    	by default while creating or upgrading database. Developers can change
    	this behavior into the styles their like. "keep" will keep the
    	cases of classes and fields. "upper" will turn all classes names
    	and fields names into uppercase. "lower" will act as default.
    	Do not change the value after you run your app for the first time,
    	or it might cause the exception that column can not be found.
    	value options: keep lower upper
    	For example:    
    	<cases value="lower" />
    -->

    <!--
        Define where the .db file should be. "internal" means the .db file
        will be stored in the database folder of internal storage which no
        one can access. "external" means the .db file will be stored in the
        path to the directory on the primary external storage device where
        the application can place persistent files it owns which everyone
        can access. "internal" will act as default.
        For example:
        <storage value="external" />
    -->

    <storage value="external" />

</litepal>

  dbname  // 数据库名称为litepaldemo,默认以 .db 结尾,如未以此结尾,则自动添加。

 version     // 数据库版本为1,每次数据库发生变动版本号必须+1

list        // 有几张表就在list之间写几个mapping  // 切记  :list标签中的实体类都应该继承LitePalSupport这个类

storage  //定义数据库文件存储的地方,可选 internal(内部) 和 external(外部), 默认为 internal

3.在application中初始化(两种方法)

     (1)如果项目中没有写BaseApplication这种基类的话,就直接在清单文件中配置 LitePalApplication,代码如下:

<manifest>
    <application
        android:name="org.litepal.LitePalApplication"
        ...
    >
        ...
    </application>
</manifest>

(2)如果项目中定义了自己的 BaseApplication,那么就直接在BaseApplication中的onCreate()方法中初始化下 LitePal就可以

public class MyApplication extends Application { 
    @Override 
    public void onCreate() { 
        super.onCreate();
        // 初始化
        LitePal.initialize(this); 
    }  
 ... 
 }

 

参考博客:

https://www.jianshu.com/p/8035eb5da7a2

https://blog.csdn.net/gpf1320253667/article/details/82819795

https://blog.csdn.net/guolin_blog/article/details/82714414

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值