An Android library that makes developers use SQLite database extremely easy.

Overview

LitePal for Android

Logo

中文文档

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.

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 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:

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

Of course you may have your own Application and has already configured here, like:

<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:

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:

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.
    ...
}
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:

<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:

SQLiteDatabase db = LitePal.getDatabase();

Now the tables will be generated automatically with SQLs like this:

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:

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:

<!--
    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:

Album album = new Album();
album.setName("album");
album.setPrice(10.99f);
album.setCover(getCoverImageBytes());
album.save();
Song song1 = new Song();
song1.setName("song1");
song1.setDuration(320);
song1.setAlbum(album);
song1.save();
Song song2 = new Song();
song2.setName("song2");
song2.setDuration(356);
song2.setAlbum(album);
song2.save();

Kotlin:

val album = Album()
album.name = "album"
album.price = 10.99f
album.cover = getCoverImageBytes()
album.save()
val song1 = Song()
song1.name = "song1"
song1.duration = 320
song1.album = album
song1.save()
val song2 = Song()
song2.name = "song2"
song2.duration = 356
song2.album = album
song2.save()

This will insert album, song1 and song2 into database with associations.

4. Update data

The simplest way, use save() method to update a record found by find().

Java:

Album albumToUpdate = LitePal.find(Album.class, 1);
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.save();

Kotlin:

val albumToUpdate = LitePal.find<Album>(1)
albumToUpdate.price = 20.99f // raise the price
albumToUpdate.save()

Each model which inherits from LitePalSupport would also have update() and updateAll() method. You can update a single record with a specified id.

Java:

Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.update(id);

Kotlin:

val albumToUpdate = Album()
albumToUpdate.price = 20.99f // raise the price
albumToUpdate.update(id)

Or you can update multiple records with a where condition.

Java:

Album albumToUpdate = new Album();
albumToUpdate.setPrice(20.99f); // raise the price
albumToUpdate.updateAll("name = ?", "album");

Kotlin:

val albumToUpdate = Album()
albumToUpdate.price = 20.99f // raise the price
albumToUpdate.updateAll("name = ?", "album")

5. Delete data

You can delete a single record using the static delete() method in LitePal.

Java:

LitePal.delete(Song.class, id);

Kotlin:

LitePal.delete<Song>(id)

Or delete multiple records using the static deleteAll() method in LitePal.

Java:

LitePal.deleteAll(Song.class, "duration > ?" , "350");

Kotlin:

LitePal.deleteAll<Song>("duration > ?" , "350")

6. Query data

Find a single record from song table with specified id.

Java:

Song song = LitePal.find(Song.class, id);

Kotlin:

val song = LitePal.find<Song>(id)

Find all records from song table.

Java:

List<Song> allSongs = LitePal.findAll(Song.class);

Kotlin:

val allSongs = LitePal.findAll<Song>()

Constructing complex query with fluent query.

Java:

List<Song> songs = LitePal.where("name like ? and duration < ?", "song%", "200").order("duration").find(Song.class);

Kotlin:

val songs = LitePal.where("name like ? and duration < ?", "song%", "200").order("duration").find<Song>()

7. Multiple databases

If your app needs multiple databases, LitePal support it completely. You can create as many databases as you want at runtime. For example:

LitePalDB litePalDB = new LitePalDB("demo2", 1);
litePalDB.addClassName(Singer.class.getName());
litePalDB.addClassName(Album.class.getName());
litePalDB.addClassName(Song.class.getName());
LitePal.use(litePalDB);

This will create a demo2 database with singer, album and song tables.

If you just want to create a new database but with same configuration as litepal.xml, you can do it with:

LitePalDB litePalDB = LitePalDB.fromDefault("newdb");
LitePal.use(litePalDB);

You can always switch back to default database with:

LitePal.useDefault();

And you can delete any database by specified database name:

LitePal.deleteDatabase("newdb");

8. Transaction

LitePal support transaction for atomic db operations. All operations in the transaction will be committed or rolled back together.

Java usage:

LitePal.beginTransaction();
boolean result1 = // db operation1
boolean result2 = // db operation2
boolean result3 = // db operation3
if (result1 && result2 && result3) {
    LitePal.setTransactionSuccessful();
}
LitePal.endTransaction();

Kotlin usage:

LitePal.runInTransaction {
    val result1 = // db operation1
    val result2 = // db operation2
    val result3 = // db operation3
    result1 && result2 && result3
}

ProGuard

If you are using ProGuard you might need to add the following option:

-keep class org.litepal.** {
    *;
}

-keep class * extends org.litepal.crud.DataSupport {
    *;
}

-keep class * extends org.litepal.crud.LitePalSupport {
    *;
}

Bugs Report

If you find any bug when using LitePal, please report here. Thanks for helping us making better.

Change logs

3.2.3

  • Support database index by adding @Column(index = true) on field.
  • Adding return value for runInTransaction() function for Kotlin.
  • Fix known bugs.

3.1.1

  • Support transaction.
  • Add return value for LitePal.saveAll() method.
  • No longer support byte array field as column in table.
  • Deprecate all async methods. You should handle async operations by yourself.
  • Fix known bugs.

3.0.0

  • Optimize generic usage for async operation APIs.
  • Add LitePal.registerDatabaseListener() method for listening create or upgrade database events.
  • Provider better CRUD API usage for using generic declaration instead of Class parameter for kotlin.
  • Fix known bugs.

2.0.0

  • Offer new APIs for CRUD operations. Deprecate DataSupport, use LitePal and LitePalSupport instead.
  • Fully support kotlin programming.
  • Fix known bugs.

1.6.1

  • Support AES and MD5 encryption with @Encrypt annotation on fields.
  • Support to store database file on any directory of external storage.
  • Fix known bugs.

1.5.1

  • Support async operations for all crud methods.
  • Add saveOrUpdate() method in DataSupport.
  • Fix known bugs.

1.4.1

  • Support multiple databases.
  • Support crud operations for generic collection data in models.
  • Add SQLite keywords convert function to avoid keywords conflict.
  • Fix bug of DateSupport.count error.
  • Fix bug of losing blob data when upgrading database.
  • Fix other known bugs.

1.3.2

  • Improve an outstanding speed up of querying and saving.
  • Support to store database file in external storage.
  • Support to mapping fields which inherit from superclass.
  • Add findFirst() and findLast() in fluent query.
  • Add isExist() and saveIfNotExist() method in DataSupport.

1.3.1

  • Support storing binary data. Byte array field will be mapped into database as blob type.
  • Add saveFast() method in DataSupport. If your model has no associations to handle, use saveFast() method will be much more efficient.
  • Improve query speed with optimized algorithm.

1.3.0

  • Add annotation functions to declare unique, not null and default constraints.
  • Remove the trick of ignore mapping fields with non-private modifier.
  • Support to use annotation to ignore mapping fields with ignore = true
  • Add some magical methods in DataSupport for those who understand LitePal deeper.
  • Fix known bugs.

License

Copyright (C) Lin Guo, LitePal Framework Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • 一次性保存10万条数据报OutOfMemoryError

    一次性保存10万条数据报OutOfMemoryError

    业务需要将10万条数据保存在本地,于是我想到使用litepal简单快捷的导入这些数据,结果报了OutOfMemoryError,保存的时候是异步的AsyncTask。

    List products=new ArrayList();//10万条数据 DataSupport.saveAll(products);

    我用saveFast()一条一条存,存了半天,但没报错。

    我将10w+条数(实际137461条数据)据分拆成1000组数据,然后for循环分批保存,使用DataSupport.saveAll(subProducts)总会碰到OutOfMemoryError的情况,是否保存完数据list没释放或其他什么原因? 看异常似乎是StringBuilder.append()的原因。

    我不知道是litepal还是google的问题,完整的异常: 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): App has crashed, executing CustomActivityOnCrash's UncaughtExceptionHandler 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): java.lang.RuntimeException: An error occured while executing doInBackground() 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at android.os.AsyncTask$3.done(AsyncTask.java:299) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.util.concurrent.FutureTask.run(FutureTask.java:239) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.lang.Thread.run(Thread.java:838) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): Caused by: java.lang.OutOfMemoryError 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.lang.StringBuilder.append(StringBuilder.java:216) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1458) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at org.litepal.crud.SaveHandler.saving(SaveHandler.java:232) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at org.litepal.crud.SaveHandler.doSaveAction(SaveHandler.java:191) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at org.litepal.crud.SaveHandler.onSaveAll(SaveHandler.java:154) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at org.litepal.crud.DataSupport.saveAll(DataSupport.java:885) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at com.app.pandian.MainActivity$ReadToDatabaseTask.doInBackground(MainActivity.java:94) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at com.app.pandian.MainActivity$ReadToDatabaseTask.doInBackground(MainActivity.java:1) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at android.os.AsyncTask$2.call(AsyncTask.java:287) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 01-03 09:25:01.876: E/CustomActivityOnCrash(2725): ... 4 more

    opened by gundumw100 16
  • 表中如果有boolean类型的字段会查询失败

    表中如果有boolean类型的字段会查询失败

    有boolean参数时,保存是可以成功的,并能查询到正确的数据数量,查询取出数据时会失败,报“Attempt to invoke virtual method 'java.lang.Class[] java.lang.reflect.Constructor.getParameterTypes()' on a null object reference org.litepal.exceptions.DataSupportException: Attempt to invoke virtual method 'java.lang.Class[] java.lang.reflect.Constructor.getParameterTypes()' on a null object reference” 错误。

    opened by Leo199206 13
  • org.litepal.exceptions.DataSupportException: getİnt [int]

    org.litepal.exceptions.DataSupportException: getİnt [int]

    Caused by: org.litepal.exceptions.DataSupportException: getİnt [int] org.litepal.crud.DataHandler.query(DataHandler.java:141) org.litepal.crud.QueryHandler.onFindAll(QueryHandler.java:122) org.litepal.crud.DataSupport.findAll(DataSupport.java:628) org.litepal.crud.DataSupport.findAll(DataSupport.java:609)

    wontfix 
    opened by ihollysee 11
  • near

    near "null": syntax error (code 1): , while compiling: INSERT INTO feed(null) VALUES (NULL)

    I have a code:

    for (Feed feed : feeds) {
        feed.save();
    }
    

    throws an android.database.sqlite.SQLiteException:

    10-10 07:13:17.105    1468-1498/me.drakeet.***** E/SQLiteLog﹕ (1) near "null": syntax error
    10-10 07:13:17.105    1468-1498/me.drakeet.***** E/SQLiteDatabase﹕ Error inserting
        android.database.sqlite.SQLiteException: near "null": syntax error (code 1): , while compiling: INSERT INTO feed(null) VALUES (NULL)
                at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
                at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
                at org.litepal.crud.SaveHandler.saving(SaveHandler.java:190)
                at org.litepal.crud.SaveHandler.doSaveAction(SaveHandler.java:151)
                at org.litepal.crud.SaveHandler.onSave(SaveHandler.java:79)
                at org.litepal.crud.DataSupport.saveThrows(DataSupport.java:1057)
                at org.litepal.crud.DataSupport.save(DataSupport.java:1020)
                at me.drakeet.*****.ui.fragment.MainFragment$2$1.doInBackground(MainFragment.java:193)
    

    Does it belong to the library bug?
    Thanks!

    opened by drakeet 10
  • 递归类创建数据库报错

    递归类创建数据库报错

    递归类代码如下:

    public class ClassifyData extends DataSupport implements Serializable{
    
        @Column(unique = true)
        private String classifyId;
        private String name;
        private List<ClassifyData>  children;
    
        public String getId() {
            return classifyId;
        }
    
        public void setId(String classifyId) {
            this.classifyId = classifyId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<ClassifyData> getChildren() {
            return children;
        }
    
        public void setChildren(List<ClassifyData> children) {
            this.children = children;
        }
    

    报错的log:

     FATAL EXCEPTION: main
    Process: com.xxx.xxxx:channel, PID: 6224
    java.lang.RuntimeException: Unable to create application com.xxx.xxxx.MyApplication: org.litepal.exceptions.DatabaseGenerateException: An exception that indicates there was an error with SQL parsing or execution. create table classifydata_classifydata (classifydata_id integer, classifydata_id integer)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5406)
    at android.app.ActivityThread.-wrap2(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
    Caused by: org.litepal.exceptions.DatabaseGenerateException: An exception that indicates there was an error with SQL parsing or execution. create table classifydata_classifydata (classifydata_id integer, classifydata_id integer)
    at org.litepal.tablemanager.Generator.execute(Generator.java:117)
    at org.litepal.tablemanager.AssociationCreator.createIntermediateTable(AssociationCreator.java:336)
    at org.litepal.tablemanager.AssociationCreator.addAssociations(AssociationCreator.java:290)
    at org.litepal.tablemanager.AssociationCreator.addOrUpdateAssociation(AssociationCreator.java:60)
    at org.litepal.tablemanager.Generator.addAssociation(Generator.java:132)
    at org.litepal.tablemanager.Generator.create(Generator.java:210)
    at org.litepal.tablemanager.LitePalOpenHelper.onCreate(LitePalOpenHelper.java:83)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
    at org.litepal.tablemanager.Connector.getWritableDatabase(Connector.java:64)
    at org.litepal.tablemanager.Connector.getDatabase(Connector.java:87)
    at com.xxx.xxxx.MyApplication.onCreate(MyApplication.kt:13)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5403)
    at android.app.ActivityThread.-wrap2(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
    
    bug 
    opened by StormKid 8
  • 关联表更新,外键丢失

    关联表更新,外键丢失

    表关联的关系是这样的: table1 1 -> n table2 分组与联系人是一对多 table2 1 -> n table3 联系人与个人资料是一对多

    现在需要将联系人从一个分组移动到另一个分组,理论上个人资料还是属于该联系人; 不过现在我将联系人对应分组的外键使用set修改(没有动表3)之后,联系人已移动到新的分组,查看数据库发现所有数据都还在,但是表3个人资料中对应联系人的外键变空了。

    另外,将联系人设置新分组时,我使用update来修改发现数据并没有更新,使用save就更新了。 (已经注意setToDefault了,没有修改为默认值的操作。

    问题1,如何让表3的外键不变? 问题2,为何不能采用update来更新外键?

    不是很清楚发生了什么,所以来请教一下郭神,请郭神不吝指教。

    环境是Android Studio 2.3.3, LitePal 版本1.6.0

    opened by huang-xinjie 8
  • myAdapter.swapCursor(someCursor) java.lang.IllegalArgumentException: column '_id' does not exist

    myAdapter.swapCursor(someCursor) java.lang.IllegalArgumentException: column '_id' does not exist

    我在用LitePal创建了一个db后,写入了一些数据,出于项目特殊要求导出db,到某个项目中使用,在一个Activity中,调用adapter.swapCursor(someCursor); 报java.lang.IllegalArgumentException: column '_id' does not exist,因为swapCursor()函数的源码中有一段是if (newCursor != null) { if (mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver); if (mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver); mRowIDColumn = newCursor.getColumnIndexOrThrow("_id"); // 主要看这行 mDataValid = true; // notify the observers about the new cursor notifyDataSetChanged(); } 我回头去看下db里面的所有列名,发现用litepal创建的表格用的是'id',而不是'_id'。 没什么意思,就是想告诉郭神有这么一回事。

    wontfix 
    opened by shanksiscool 8
  • Android 9.0 数据库文件替换后,无法读取数据库内容

    Android 9.0 数据库文件替换后,无法读取数据库内容

    在android 9.0以下版本都是正常的

    具体功能是:数据库备份到SD卡中,然后再将SD卡中的数据库.db文件替换应用内部的数据库文件。

    其中的路径都是正确的。

    成功替换后,甚至在AS的Device explorer 体积大小都是正确的,但是应用似乎没有读取这个数据库。

    Google一圈似乎是andriod WAL特性,但不知道如何解决?求教

    相关地址:https://blog.csdn.net/u010913414/article/details/88076794 https://blog.csdn.net/dtskyline/article/details/88781655 https://github.com/xpbrew/cordova-sqlite-storage/issues/810

    opened by ihewro 7
  • 死锁问题

    死锁问题

    主线程 "main" prio=5 tid=1 Blocked | group="main" sCount=1 dsCount=0 obj=0x746f9a50 self=0xf4736a00 | sysTid=17043 nice=0 cgrp=bg_non_interactive sched=0/0 handle=0xf71b8de4 | state=S schedstat=( 1381247238 86682835 492 ) utm=127 stm=11 core=5 HZ=100 | stack=0xff657000-0xff659000 stackSize=8MB | held mutexes= at com.meizu.lifekit.entity.UserSingleton.(UserSingleton.java:22)

    • waiting to lock <0x0fd0e4f2> (a java.lang.Class<org.litepal.crud.DataSupport>) held by thread 16 at com.meizu.lifekit.entity.UserSingleton.(UserSingleton.java:21) at com.meizu.lifekit.entity.UserSingleton$InstanceHolder.(UserSingleton.java:180) at com.meizu.lifekit.entity.UserSingleton.getInstance(UserSingleton.java:30) at com.meizu.lifekit.data.mainpage.NewHomeFragment.(NewHomeFragment.java:82) at java.lang.Class.newInstance!(Native method)

    TID16: "LifeKitApplication" prio=5 tid=16 Blocked | group="main" sCount=1 dsCount=0 obj=0x132db8e0 self=0xf395b900 | sysTid=17067 nice=0 cgrp=bg_non_interactive sched=0/0 handle=0xd9418930 | state=S schedstat=( 2762923 1967693 9 ) utm=0 stm=0 core=1 HZ=100 | stack=0xd9316000-0xd9318000 stackSize=1038KB | held mutexes= kernel: __switch_to+0x74/0x8c kernel: futex_wait_queue_me+0xd8/0x168 kernel: futex_wait+0xe4/0x234 kernel: do_futex+0x184/0xa14 kernel: compat_SyS_futex+0x7c/0x168 kernel: el0_svc_naked+0x20/0x28 native: #00 pc 00017698 /system/lib/libc.so (syscall+28) native: #01 pc 000e8985 /system/lib/libart.so (_ZN3art17ConditionVariable4WaitEPNS_6ThreadE+80) native: #02 pc 0029fb2b /system/lib/libart.so (_ZN3art7Monitor4LockEPNS_6ThreadE+394) native: #03 pc 002a25cb /system/lib/libart.so (_ZN3art7Monitor12MonitorEnterEPNS_6ThreadEPNS_6mirror6ObjectE+270) native: #04 pc 002d775d /system/lib/libart.so (_ZN3art10ObjectLockINS_6mirror6ObjectEEC2EPNS_6ThreadENS_6HandleIS2_EE+24) native: #05 pc 0012bfdf /system/lib/libart.so (_ZN3art11ClassLinker15InitializeClassEPNS_6ThreadENS_6HandleINS_6mirror5ClassEEEbb.part.593+94) native: #06 pc 0012ce93 /system/lib/libart.so (_ZN3art11ClassLinker17EnsureInitializedEPNS_6ThreadENS_6HandleINS_6mirror5ClassEEEbb+82) native: #07 pc 002aea0d /system/lib/libart.so (_ZN3artL18Class_classForNameEP7_JNIEnvP7_jclassP8_jstringhP8_jobject+452) native: #08 pc 0025eb19 /data/dalvik-cache/arm/system@[email protected] (Java_java_lang_Class_classForName__Ljava_lang_String_2ZLjava_lang_ClassLoader_2+132) at java.lang.Class.classForName!(Native method)

    • waiting to lock <0x081409ec> (a java.lang.Class<com.meizu.lifekit.entity.User>) held by thread 1 at java.lang.Class.forName(Class.java:324) at java.lang.Class.forName(Class.java:285) at org.litepal.LitePalBase.getSupportedFields(LitePalBase.java:170) at org.litepal.crud.DataHandler.query(DataHandler.java:124) at org.litepal.crud.QueryHandler.onFindLast(QueryHandler.java:96) at org.litepal.crud.DataSupport.findLast(DataSupport.java:576)
    • locked <0x0fd0e4f2> (a java.lang.Class<org.litepal.crud.DataSupport>) at org.litepal.crud.DataSupport.findLast(DataSupport.java:561)
    • locked <0x0fd0e4f2> (a java.lang.Class<org.litepal.crud.DataSupport>) at com.meizu.lifekit.LifeKitApplication$ApplicationHandler.handleMessage(LifeKitApplication.java:117) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.os.HandlerThread.run(HandlerThread.java:61)

    表User类实例化时,同时查询数据库,互相持有锁造成死锁 User实例化时持有User class锁申请父类DataSupport class锁,查询User表时持有DataSupport class锁申请User class锁,互相持有对方资源不释放死锁了。

    opened by laizuling 7
  • LitePalApplication 这个必须继承有点难为人啊

    LitePalApplication 这个必须继承有点难为人啊

    LitePalApplication中

    '''public static Context getContext() { if (mContext == null) { throw new GlobalException(GlobalException.APPLICATION_CONTEXT_IS_NULL); } return mContext; }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mContext = getApplicationContext();
    }'''
    

    就这一个功能还要用一个Application,太浪费了。 为嘛不要自己去set呢

    opened by ownwell 7
  • 查询bug,有时候查询不到。

    查询bug,有时候查询不到。

    数据库中存在这一条数据,但是打开应用有时候可以查询得到,有时候查询不到。关键是查询的是用户的登录信息,导致有的用户已经登录了,但是关闭应用重新打开应用又需要登录,这导致很多用户说我窃取他们的信息,真的好坑,查询也是最简单的。

    public LoginUserModel getSelectedLoginUser() {
            try {
                return LitePal.where("isSelected=1").findFirst(LoginUserModel.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    

    image

    opened by Moster1h 6
  • org.litepal.exceptions.InvalidAttributesException: dbname is empty or not defined in litepal.xml file, or your litepal.xml file is missing.

    org.litepal.exceptions.InvalidAttributesException: dbname is empty or not defined in litepal.xml file, or your litepal.xml file is missing.

    org.litepal.exceptions.InvalidAttributesException: dbname is empty or not defined in litepal.xml file, or your litepal.xml file is missing.这个问题 网上说的点都找过了 没有问题 但还是报错

    opened by FEI1111 0
  • 索引查询单个结果速度确实快,但是有多个结果时时间和没索引一样?

    索引查询单个结果速度确实快,但是有多个结果时时间和没索引一样?

    我业务中查询的是当天的数据例如LitePal.where( "updateDay=?", “2022-09-19” ).find(SectorData.Sector::class.java),查询结果会有多条,发现使用findFirst查询速度极快,但是使用.find()查询所有符合的数据是没有索引效果的

    opened by chenhan1995 3
Owner
Lin Guo
Google Developer Expert for Android
Lin Guo
LiteOrm is a fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line of code efficiently.

#LiteOrm:Android高性能数据库框架 A fast, small, powerful ORM framework for Android. LiteOrm makes you do CRUD operarions on SQLite database with a sigle line

马天宇 1.5k Nov 19, 2022
SquiDB is a SQLite database library for Android and iOS

Most ongoing development is currently taking place on the dev_4.0 branch. Click here to see the latest changes and try out the 4.0 beta. Introducing S

Yahoo 1.3k Dec 26, 2022
AndroidQuery is an Android ORM for SQLite and ContentProvider which focuses on easy of use and performances thanks to annotation processing and code generation

WARNING: now that Room is out, I no longer maintain that library. If you need a library to easy access to default android ContentProvider, I would may

Frédéric Julian 19 Dec 11, 2021
Compile time processed, annotation driven, no reflection SQLite database layer for Android

SqliteMagic Simple yet powerful SQLite database layer for Android that makes database handling feel like magic. Overview: Simple, intuitive & typesafe

Siim Kinks 118 Dec 22, 2022
android 数据库框架,sqlite database

DBExecutor 主要的功能 1.使用了读写锁,支持多线程操作数据。 2.支持事务 3.支持ORM 4.缓存Sql,缓存表结构 这个类库主要用于android 数据库操作。 始终围绕着一个类对应一个表的概念。 只要创建一个实体类,就不用当心它怎么存储在数据库中,不用重新写增删改查的代码。基本的功

null 77 May 31, 2021
Realm is a mobile database: a replacement for SQLite & ORMs

Realm is a mobile database that runs directly inside phones, tablets or wearables. This repository holds the source code for the Java version of Realm

Realm 11.4k Jan 2, 2023
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
A blazing fast, powerful, and very simple ORM android database library that writes database code for you.

README DBFlow is fast, efficient, and feature-rich Kotlin database library built on SQLite for Android. DBFlow utilizes annotation processing to gener

Andrew Grosner 4.9k Dec 30, 2022
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.

Thanks to JetBrains for support Kripton Persistence Library project! Kripton Persistence Library Kripton is a java library, for Android platform, that

xcesco 117 Nov 11, 2022
Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

null 2.6k Dec 16, 2022
Insanely easy way to work with Android Database.

Sugar ORM Insanely easy way to work with Android databases. Official documentation can be found here - Check some examples below. The example applicat

null 2.6k Jan 9, 2023
A simple ToDo app to demonstrate the use of Realm Database in android to perform some basic CRUD operations like Create, Update and Delete.

Creating a Realm Model Class @RealmClass open class Note() : RealmModel { @PrimaryKey var id: String = "" @Required var title: String

Joel Kanyi 15 Dec 18, 2022
Active record style SQLite persistence for Android

ActiveAndroid ActiveAndroid is an active record style ORM (object relational mapper). What does that mean exactly? Well, ActiveAndroid allows you to s

Michael Pardo 4.7k Dec 29, 2022
greenDAO is a light & fast ORM solution for Android that maps objects to SQLite databases.

Check out ObjectBox Check out our new mobile database ObjectBox (GitHub repo). ObjectBox is a superfast object-oriented database with strong relation

Markus Junginger 12.6k Jan 3, 2023
Active record style SQLite persistence for Android

ActiveAndroid ActiveAndroid is an active record style ORM (object relational mapper). What does that mean exactly? Well, ActiveAndroid allows you to s

Michael Pardo 4.7k Dec 29, 2022
Android SQLite API based on SQLCipher

Download Source and Binaries The latest AAR binary package information can be here, the source can be found here. Compatibility SQLCipher for Android

SQLCipher 2.6k Dec 31, 2022
Extended SQLite functionality for Android

sqlite-provider A simplification of database access for Android. Description sqlite-provider implements a ContentProvider for you that allows database

Novoda 308 Nov 20, 2022
lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)

Description ORMAN is an minimalistic and lightweight ORM framework for Java which can handle your common database usage without writing SQL and strugg

Ahmet Alp Balkan 246 Nov 20, 2022
lightweight and minimalist ORM for Java/Android. works with SQLite & MySQL. (not actively maintained)

Description ORMAN is an minimalistic and lightweight ORM framework for Java which can handle your common database usage without writing SQL and strugg

Ahmet Alp Balkan 246 Nov 20, 2022