# LitePal for Android
![Logo](https://github.com/LitePalFramework/LitePal/blob/master/sample/src/main/logo/mini_logo.png)
[中文文档](https://blog.csdn.net/sinyu890807/category_9262963.html)
LitePal is an open source Android library that allows developers to use SQLite database extremely easy. You can finish most of the database operations without writing even a SQL statement, including create or upgrade tables, crud operations, aggregate functions, etc. The setup of LitePal is quite simple as well, you can integrate it into your project in less than 5 minutes.
Experience the magic right now and have fun!
## Features
* Using object-relational mapping (ORM) pattern.
* Almost zero-configuration(only one configuration file with few properties).
* Maintains all tables automatically(e.g. create, alter or drop tables).
* Multi databases supported.
* Encapsulated APIs for avoiding writing SQL statements.
* Awesome fluent query API.
* Alternative choice to use SQL still, but easier and better APIs than the originals.
* More for you to explore.
## Quick Setup
#### 1. Include library
Edit your **build.gradle** file and add below dependency.
``` groovy
dependencies {
implementation 'org.litepal.guolindev:core:3.2.3'
}
```
#### 2. Configure litepal.xml
Create a file in the **assets** folder of your project and name it as **litepal.xml**. Then copy the following codes into it.
``` 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="demo" />
<!--
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="1" />
<!--
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>
</list>
<!--
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" />
-->
</litepal>
```
This is the only configuration file, and the properties are simple.
* **dbname** configure the database name of project.
* **version** configure the version of database. Each time you want to upgrade database, plus the value here.
* **list** configure the mapping classes.
* **storage** configure where the database file should be stored. **internal** and **external** are the only valid options.
#### 3. Configure LitePalApplication
You don't want to pass the Context param all the time. To makes the APIs simple, just configure the LitePalApplication in **AndroidManifest.xml** as below:
``` xml
<manifest>
<application
android:name="org.litepal.LitePalApplication"
...
>
...
</application>
</manifest>
```
Of course you may have your own Application and has already configured here, like:
``` xml
<manifest>
<application
android:name="com.example.MyOwnApplication"
...
>
...
</application>
</manifest>
```
That's OK. LitePal can still live with that. Just call **LitePal.initialize(context)** in your own Application:
```java
public class MyOwnApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
LitePal.initialize(this);
}
...
}
```
Make sure to call this method as early as you can. In the **onCreate()** method of Application will be fine. And always remember to use the application context as parameter. Do not use any instance of activity or service as parameter, or memory leaks might happen.
## Get Started
After setup, you can experience the powerful functions now.
#### 1. Create tables
Define the models first. For example you have two models, **Album** and **Song**. The models can be defined as below:
``` java
public class Album extends LitePalSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
@Column(index = true)
private float price;
private List<Song> songs = new ArrayList<>();
// generated getters and setters.
...
}
```
``` java
public class Song extends LitePalSupport {
@Column(nullable = false)
private String name;
private int duration;
@Column(ignore = true)
private String uselessField;
private Album album;
// generated getters and setters.
...
}
```
Then add these models into the mapping list in **litepal.xml**:
``` xml
<list>
<mapping class="org.litepal.litepalsample.model.Album" />
<mapping class="org.litepal.litepalsample.model.Song" />
</list>
```
OK! The tables will be generated next time you operate database. For example, gets the **SQLiteDatabase** with following codes:
``` java
SQLiteDatabase db = LitePal.getDatabase();
```
Now the tables will be generated automatically with SQLs like this:
``` sql
CREATE TABLE album (
id integer primary key autoincrement,
name text unique default 'unknown',
price real
);
CREATE TABLE song (
id integer primary key autoincrement,
name text not null,
duration integer,
album_id integer
);
```
#### 2. Upgrade tables
Upgrade tables in LitePal is extremely easy. Just modify your models anyway you want:
```java
public class Album extends LitePalSupport {
@Column(unique = true, defaultValue = "unknown")
private String name;
@Column(ignore = true)
private float price;
private Date releaseDate;
private List<Song> songs = new ArrayList<>();
// generated getters and setters.
...
}
```
A **releaseDate** field was added and **price** field was annotated to ignore.
Then increase the version number in **litepal.xml**:
```xml
<!--
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" />
```
The tables will be upgraded next time you operate database. A **releasedate** column will be added into **album** table and the original **price** column will be removed. All the data in **album** table except those removed columns will be retained.
But there are some upgrading conditions that LitePal can't handle and all data in the upgrading table will be cleaned:
* Add a field which annotated as `unique = true`.
* Change a field's annotation into `unique = true`.
* Change a field's annotation into `nullable = false`.
Be careful of the above conditions which will cause losing data.
#### 3. Save data
The saving API is quite object oriented. Each model which inherits from **LitePalSupport** would have the **save()** method for free.
Java:
``` java
Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();
Song song1 = new Song();
son
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
LitePal是一个开源的Android库,它允许开发人员非常容易地使用SQLite数据库 (234个子文件)
gradlew.bat 2KB
.gitignore 387B
.gitignore 7B
.gitignore 7B
.gitignore 7B
.gitignore 7B
bintray.gradle 2KB
bintray.gradle 2KB
build.gradle 1KB
build.gradle 723B
build.gradle 686B
build.gradle 509B
build.gradle 492B
settings.gradle 24B
gradlew 5KB
litepal-2.0.0-src.jar 375KB
litepal-1.6.1-src.jar 309KB
litepal-1.6.0-src.jar 308KB
litepal-1.5.1-src.jar 296KB
litepal-2.0.0.jar 233KB
litepal-1.4.1-src.jar 228KB
litepal-1.4.0-src.jar 228KB
litepal-1.3.2-src.jar 200KB
litepal-1.3.1-src.jar 197KB
litepal-1.3.0-src.jar 193KB
litepal-1.2.1-src.jar 185KB
litepal-1.2.0-src.jar 185KB
litepal-1.1.1-src.jar 182KB
litepal-1.1.0-src.jar 182KB
litepal-1.6.1.jar 176KB
litepal-1.6.0.jar 176KB
litepal-1.5.1.jar 169KB
litepal-1.4.0.jar 114KB
litepal-1.4.1.jar 114KB
litepal-1.3.2.jar 98KB
litepal-1.3.1.jar 97KB
litepal-1.3.0.jar 95KB
litepal-1.2.0.jar 91KB
litepal-1.2.1.jar 91KB
litepal-1.1.1.jar 89KB
litepal-1.1.0.jar 89KB
gradle-wrapper.jar 49KB
Operator.java 59KB
DataHandler.java 53KB
LitePal.java 45KB
FluentQuery.java 30KB
LitePalBase.java 30KB
LitePalSupport.java 27KB
DBUtility.java 22KB
SaveHandler.java 20KB
AssociationUpdater.java 18KB
UpdateUsingUpdateMethodTest.java 18KB
DeleteTest.java 18KB
UpdateHandler.java 18KB
AssociationCreator.java 18KB
DeleteHandler.java 15KB
QueryClusterTest.java 15KB
Upgrader.java 15KB
UpdateUsingSaveMethodTest.java 14KB
QueryHandler.java 10KB
LitePalTestCase.java 10KB
SaveTest.java 9KB
BaseUtility.java 8KB
Many2ManyAnalyzer.java 8KB
Generator.java 8KB
AESCrypt.java 7KB
AssociationsAnalyzer.java 7KB
TransactionTest.java 7KB
Many2OneAnalyzer.java 7KB
LitePalParser.java 7KB
QueryEagerTest.java 7KB
SaveAllTest.java 6KB
QueryBasicTest.java 6KB
LitePalAttr.java 6KB
UpdateSampleActivity.java 5KB
AssociationsInfo.java 5KB
DeleteSampleActivity.java 5KB
DynamicExecutor.java 5KB
SaveSampleActivity.java 5KB
Many2ManySaveTest.java 5KB
MultiDatabaseTest.java 5KB
One2OneAnalyzer.java 5KB
AssociationsModel.java 4KB
Dropper.java 4KB
Creator.java 4KB
LitePalOpenHelper.java 4KB
Connector.java 4KB
LitePalDB.java 4KB
TableStructureActivity.java 4KB
ModelListActivity.java 4KB
QueryMathTest.java 4KB
LitePalSupportException.java 4KB
LitePalContentHandler.java 3KB
SharedUtil.java 3KB
TableModel.java 3KB
ModelStructureActivity.java 3KB
CipherUtil.java 3KB
Many2OneBiSaveTest.java 3KB
TableListActivity.java 3KB
ColumnModel.java 3KB
共 234 条
- 1
- 2
- 3
资源评论
hello_中年人
- 粉丝: 7
- 资源: 324
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功