Is it possible to use StringFormat or Constant Variable in android Room Query












0















I want to query the user associations list with the following room query using public constant variable Association.MEMBER_STATUS_APPROVED.



@Query("SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED)
LiveData<List<Association>> loadUserAssociations();


But, room gives me [SQLITE_ERROR] when build.
It is possible to re-write that query by replacing the constant variable with parameter like the following.



@Query("SELECT * FROM Association WHERE memberStatus = :statusApproved")
LiveData<List<Association>> loadUserAssociations(String statusApproved);


I would like to know that does Room support such kind of string concatenation or String Format? (or) May be I missing something?










share|improve this question

























  • the 2nd one should work.. is there any error?

    – DKV
    Nov 20 '18 at 5:42











  • Yes, @DKV. The 2nd one works. But, I prefer the 1st one and I just want to know does Room support such kind of string concatenation or string format.

    – Toe Lie
    Nov 20 '18 at 5:47











  • No, it's not possible with first type.

    – Jeel Vankhede
    Nov 20 '18 at 5:49











  • Thanks @JeelVankhede. I think Room should support like that :-D How do you think? :-)

    – Toe Lie
    Nov 20 '18 at 5:56











  • No, actually it's not fault of ROOM, but you can't pass variables to Annotations it must be static final value.

    – Jeel Vankhede
    Nov 20 '18 at 6:06
















0















I want to query the user associations list with the following room query using public constant variable Association.MEMBER_STATUS_APPROVED.



@Query("SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED)
LiveData<List<Association>> loadUserAssociations();


But, room gives me [SQLITE_ERROR] when build.
It is possible to re-write that query by replacing the constant variable with parameter like the following.



@Query("SELECT * FROM Association WHERE memberStatus = :statusApproved")
LiveData<List<Association>> loadUserAssociations(String statusApproved);


I would like to know that does Room support such kind of string concatenation or String Format? (or) May be I missing something?










share|improve this question

























  • the 2nd one should work.. is there any error?

    – DKV
    Nov 20 '18 at 5:42











  • Yes, @DKV. The 2nd one works. But, I prefer the 1st one and I just want to know does Room support such kind of string concatenation or string format.

    – Toe Lie
    Nov 20 '18 at 5:47











  • No, it's not possible with first type.

    – Jeel Vankhede
    Nov 20 '18 at 5:49











  • Thanks @JeelVankhede. I think Room should support like that :-D How do you think? :-)

    – Toe Lie
    Nov 20 '18 at 5:56











  • No, actually it's not fault of ROOM, but you can't pass variables to Annotations it must be static final value.

    – Jeel Vankhede
    Nov 20 '18 at 6:06














0












0








0








I want to query the user associations list with the following room query using public constant variable Association.MEMBER_STATUS_APPROVED.



@Query("SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED)
LiveData<List<Association>> loadUserAssociations();


But, room gives me [SQLITE_ERROR] when build.
It is possible to re-write that query by replacing the constant variable with parameter like the following.



@Query("SELECT * FROM Association WHERE memberStatus = :statusApproved")
LiveData<List<Association>> loadUserAssociations(String statusApproved);


I would like to know that does Room support such kind of string concatenation or String Format? (or) May be I missing something?










share|improve this question
















I want to query the user associations list with the following room query using public constant variable Association.MEMBER_STATUS_APPROVED.



@Query("SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED)
LiveData<List<Association>> loadUserAssociations();


But, room gives me [SQLITE_ERROR] when build.
It is possible to re-write that query by replacing the constant variable with parameter like the following.



@Query("SELECT * FROM Association WHERE memberStatus = :statusApproved")
LiveData<List<Association>> loadUserAssociations(String statusApproved);


I would like to know that does Room support such kind of string concatenation or String Format? (or) May be I missing something?







android database android-sqlite android-room






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 7:18









Alireza Noorali

1,393634




1,393634










asked Nov 20 '18 at 5:39









Toe LieToe Lie

12




12













  • the 2nd one should work.. is there any error?

    – DKV
    Nov 20 '18 at 5:42











  • Yes, @DKV. The 2nd one works. But, I prefer the 1st one and I just want to know does Room support such kind of string concatenation or string format.

    – Toe Lie
    Nov 20 '18 at 5:47











  • No, it's not possible with first type.

    – Jeel Vankhede
    Nov 20 '18 at 5:49











  • Thanks @JeelVankhede. I think Room should support like that :-D How do you think? :-)

    – Toe Lie
    Nov 20 '18 at 5:56











  • No, actually it's not fault of ROOM, but you can't pass variables to Annotations it must be static final value.

    – Jeel Vankhede
    Nov 20 '18 at 6:06



















  • the 2nd one should work.. is there any error?

    – DKV
    Nov 20 '18 at 5:42











  • Yes, @DKV. The 2nd one works. But, I prefer the 1st one and I just want to know does Room support such kind of string concatenation or string format.

    – Toe Lie
    Nov 20 '18 at 5:47











  • No, it's not possible with first type.

    – Jeel Vankhede
    Nov 20 '18 at 5:49











  • Thanks @JeelVankhede. I think Room should support like that :-D How do you think? :-)

    – Toe Lie
    Nov 20 '18 at 5:56











  • No, actually it's not fault of ROOM, but you can't pass variables to Annotations it must be static final value.

    – Jeel Vankhede
    Nov 20 '18 at 6:06

















the 2nd one should work.. is there any error?

– DKV
Nov 20 '18 at 5:42





the 2nd one should work.. is there any error?

– DKV
Nov 20 '18 at 5:42













Yes, @DKV. The 2nd one works. But, I prefer the 1st one and I just want to know does Room support such kind of string concatenation or string format.

– Toe Lie
Nov 20 '18 at 5:47





Yes, @DKV. The 2nd one works. But, I prefer the 1st one and I just want to know does Room support such kind of string concatenation or string format.

– Toe Lie
Nov 20 '18 at 5:47













No, it's not possible with first type.

– Jeel Vankhede
Nov 20 '18 at 5:49





No, it's not possible with first type.

– Jeel Vankhede
Nov 20 '18 at 5:49













Thanks @JeelVankhede. I think Room should support like that :-D How do you think? :-)

– Toe Lie
Nov 20 '18 at 5:56





Thanks @JeelVankhede. I think Room should support like that :-D How do you think? :-)

– Toe Lie
Nov 20 '18 at 5:56













No, actually it's not fault of ROOM, but you can't pass variables to Annotations it must be static final value.

– Jeel Vankhede
Nov 20 '18 at 6:06





No, actually it's not fault of ROOM, but you can't pass variables to Annotations it must be static final value.

– Jeel Vankhede
Nov 20 '18 at 6:06












1 Answer
1






active

oldest

votes


















0















  • problem


if you make a query like this,




"SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED




it's just




"SELECT * FROM Association WHERE memberStatus = somevalue"




sql cannot know "somvalue" is a string.






  • try this (wrap it with quotation mark)



"SELECT * FROM Association WHERE memberStatus = '" + Association.MEMBER_STATUS_APPROVED + "'"







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386872%2fis-it-possible-to-use-stringformat-or-constant-variable-in-android-room-query%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    • problem


    if you make a query like this,




    "SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED




    it's just




    "SELECT * FROM Association WHERE memberStatus = somevalue"




    sql cannot know "somvalue" is a string.






    • try this (wrap it with quotation mark)



    "SELECT * FROM Association WHERE memberStatus = '" + Association.MEMBER_STATUS_APPROVED + "'"







    share|improve this answer




























      0















      • problem


      if you make a query like this,




      "SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED




      it's just




      "SELECT * FROM Association WHERE memberStatus = somevalue"




      sql cannot know "somvalue" is a string.






      • try this (wrap it with quotation mark)



      "SELECT * FROM Association WHERE memberStatus = '" + Association.MEMBER_STATUS_APPROVED + "'"







      share|improve this answer


























        0












        0








        0








        • problem


        if you make a query like this,




        "SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED




        it's just




        "SELECT * FROM Association WHERE memberStatus = somevalue"




        sql cannot know "somvalue" is a string.






        • try this (wrap it with quotation mark)



        "SELECT * FROM Association WHERE memberStatus = '" + Association.MEMBER_STATUS_APPROVED + "'"







        share|improve this answer














        • problem


        if you make a query like this,




        "SELECT * FROM Association WHERE memberStatus = " + Association.MEMBER_STATUS_APPROVED




        it's just




        "SELECT * FROM Association WHERE memberStatus = somevalue"




        sql cannot know "somvalue" is a string.






        • try this (wrap it with quotation mark)



        "SELECT * FROM Association WHERE memberStatus = '" + Association.MEMBER_STATUS_APPROVED + "'"








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 6:12









        yeonseok.seoyeonseok.seo

        1,03469




        1,03469






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386872%2fis-it-possible-to-use-stringformat-or-constant-variable-in-android-room-query%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith