Code Monkey home page Code Monkey logo

android-content-resolver-sql's Introduction

Content Resolver SQL

platform-badge minsdk-badge paypal-badge

Allows to write SQL statements instead of using contentResolver.query(...). The library add an extension function to ContentResolver named querySql(query: String, selectionArgs: Array<String>? = 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:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

implementation 'com.github.ologe:android-content-resolver-SQL:1.2.2'

Example 1

Get all songs

val query = """ 
    SELECT *
    FROM ${Media.EXTERNAL_CONTENT_URI}
""" 
contentResolver.querySql(query)

instead of

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

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

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"
)

android-content-resolver-sql's People

Contributors

ologe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

xing1p runforlife

android-content-resolver-sql's Issues

Not Working !!!

I have used this classes and query the content resolver but still it it creating SQL query like this :
SELECT bucket_display_name, _display_name, _size, datetaken, _id FROM images WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' , '0df8-0b0a' ))) AND ((_size < 1309090) GROUP BY (bucket_display_name)) ORDER BY _size DESC

and showing error : android.database.sqlite.SQLiteException: near "GROUP": syntax error (code 1 SQLITE_ERROR): , while compiling: SELECT bucket_display_name, _display_name, _size, datetaken, _id FROM images WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' , '0df8-0b0a' ))) AND ((_size < 1309090) GROUP BY (bucket_display_name)) ORDER BY _size DESC

this also happens when adding the parenthesis manually before the Group and After it but nothing worked!!
Kindly Help,
Thanks in Advance

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.