https://github.com/ologe/android-content-resolver-sql
One function library that allows to use SQL statements on ContentResolver class
https://github.com/ologe/android-content-resolver-sql
android android-library contentprovider sql sqlite sqlite-android
Last synced: 10 months ago
JSON representation
One function library that allows to use SQL statements on ContentResolver class
- Host: GitHub
- URL: https://github.com/ologe/android-content-resolver-sql
- Owner: ologe
- License: mit
- Created: 2019-05-13T18:18:28.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-11T14:09:19.000Z (almost 6 years ago)
- Last Synced: 2025-03-25T11:49:20.616Z (10 months ago)
- Topics: android, android-library, contentprovider, sql, sqlite, sqlite-android
- Language: Kotlin
- Homepage:
- Size: 162 KB
- Stars: 5
- Watchers: 0
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
[github]: https://github.com/ologe/android-content-resolver-SQL
[paypal-url]: https://paypal.me/nextmusicplayer
[platform-badge]: https://img.shields.io/badge/Platform-Android-F3745F.svg
[paypal-badge]: https://img.shields.io/badge/Donate-Paypal-F3745F.svg
[minsdk-badge]: https://img.shields.io/badge/minSdkVersion-16-F3745F.svg
Content Resolver SQL
=
[![platform-badge]][github]
[![minsdk-badge]][github]
[![paypal-badge]][paypal-url]
[](https://jitpack.io/#ologe/android-content-resolver-SQL)
Allows to write SQL statements instead of using `contentResolver.query(...)`.
The library add an extension function to ContentResolver named
`querySql(query: String, selectionArgs: Array? = null)`
### Limitations
- When using `LIMIT` keyword, you need to specify also `ORDER BY`.
- When using `GROUP BY` keyword, you need to specify also `WHERE`.
- You can use `WHERE 1 GROUP BY` if you haven’t a `WHERE` condition
- `JOIN` are not supported by android content provider itself.
## Getting started
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```
Step 2. Add the dependency
```groovy
implementation 'com.github.ologe:android-content-resolver-SQL:1.2.2'
```
## Example 1
**Get all songs**
```kotlin
val query = """
SELECT *
FROM ${Media.EXTERNAL_CONTENT_URI}
"""
contentResolver.querySql(query)
```
**instead of**
```kotlin
contentResolver.query(Media.EXTERNAL_CONTENT_URI, null, null, null, null)
```
## Example 2
**Get first 10 artists offsetted by 2, that have at least 5 tracks and 2 albums, excluding the podcast, ordered by artist desc**
```kotlin
val query = """
SELECT distinct $ARTIST_ID, $ARTIST, count(*) as songs, count(distinct $ALBUM_ID) as albums
FROM ${Media.EXTERNAL_CONTENT_URI}
WHERE $IS_PODCAST = 0
GROUP BY $ARTIST_ID
HAVING songs >= 5 AND albums >= 2
ORDER BY $ARTIST_KEY DESC
LIMIT 10
OFFSET 2
"""
contentResolver.querySql(query)
```
**instead of**
```kotlin
contentResolver.query(
uri = Media.EXTERNAL_CONTENT_URI,
projection = arrayOf(
"distinct $ARTIST_ID",
ARTIST,
"count(*) as songs",
"count(distinct $ALBUM_ID) as albums"
),
selection = "$IS_PODCAST = 0 ) GROUP BY $ARTIST_ID HAVING (songs >= 5 AND albums >= 2",
selectionArgs = null,
sortOrder = "$ARTIST_KEY DESC LIMIT 20 OFFSET 2"
)
```