How to return a string value from sqlite query?












0















Here is my query. I want to get the string value and return the single value to the activity class from where I have called the getSelectedMerchantCode function. My code is not working. Its returning "error"



public String getSelectedMerchantCode(String merchantname){
String selection = "Error";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT merchantCode FROM " + "merchantList"+ " WHERE " + "merchantName" + "='" + merchantname + "'", null);
if(c.getCount() == 1){
c.moveToFirst();
selection = c.getString(c.getColumnIndex("merchantCode"));
return selection;
}
c.close();
db.close();

return null;
}









share|improve this question




















  • 3





    what is the error ? post error log

    – Ganesh Gudghe
    Nov 21 '18 at 8:59













  • Because i actually don't want the row ID. i want a specific code which is in the table according to the merchantName. @pskink

    – nawshin.ahmed
    Nov 21 '18 at 9:00













  • @GaneshGudghe there is no error. its returning the string "error" which i have declared in the code.

    – nawshin.ahmed
    Nov 21 '18 at 9:01













  • Yes. I have tried onItemClickListener. Its getting the selected name. Now i want the specific code of that selected name for which i don't knw what to do. I am now basically want to run a query, get the code, then insert it to a new table.

    – nawshin.ahmed
    Nov 21 '18 at 9:04











  • @VladyslavMatviienko "error" is just a string which i have declared in the fucntion.

    – nawshin.ahmed
    Nov 21 '18 at 9:06
















0















Here is my query. I want to get the string value and return the single value to the activity class from where I have called the getSelectedMerchantCode function. My code is not working. Its returning "error"



public String getSelectedMerchantCode(String merchantname){
String selection = "Error";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT merchantCode FROM " + "merchantList"+ " WHERE " + "merchantName" + "='" + merchantname + "'", null);
if(c.getCount() == 1){
c.moveToFirst();
selection = c.getString(c.getColumnIndex("merchantCode"));
return selection;
}
c.close();
db.close();

return null;
}









share|improve this question




















  • 3





    what is the error ? post error log

    – Ganesh Gudghe
    Nov 21 '18 at 8:59













  • Because i actually don't want the row ID. i want a specific code which is in the table according to the merchantName. @pskink

    – nawshin.ahmed
    Nov 21 '18 at 9:00













  • @GaneshGudghe there is no error. its returning the string "error" which i have declared in the code.

    – nawshin.ahmed
    Nov 21 '18 at 9:01













  • Yes. I have tried onItemClickListener. Its getting the selected name. Now i want the specific code of that selected name for which i don't knw what to do. I am now basically want to run a query, get the code, then insert it to a new table.

    – nawshin.ahmed
    Nov 21 '18 at 9:04











  • @VladyslavMatviienko "error" is just a string which i have declared in the fucntion.

    – nawshin.ahmed
    Nov 21 '18 at 9:06














0












0








0








Here is my query. I want to get the string value and return the single value to the activity class from where I have called the getSelectedMerchantCode function. My code is not working. Its returning "error"



public String getSelectedMerchantCode(String merchantname){
String selection = "Error";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT merchantCode FROM " + "merchantList"+ " WHERE " + "merchantName" + "='" + merchantname + "'", null);
if(c.getCount() == 1){
c.moveToFirst();
selection = c.getString(c.getColumnIndex("merchantCode"));
return selection;
}
c.close();
db.close();

return null;
}









share|improve this question
















Here is my query. I want to get the string value and return the single value to the activity class from where I have called the getSelectedMerchantCode function. My code is not working. Its returning "error"



public String getSelectedMerchantCode(String merchantname){
String selection = "Error";
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("SELECT merchantCode FROM " + "merchantList"+ " WHERE " + "merchantName" + "='" + merchantname + "'", null);
if(c.getCount() == 1){
c.moveToFirst();
selection = c.getString(c.getColumnIndex("merchantCode"));
return selection;
}
c.close();
db.close();

return null;
}






android sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 8:58









Jaymin

1,6792927




1,6792927










asked Nov 21 '18 at 8:55









nawshin.ahmednawshin.ahmed

107




107








  • 3





    what is the error ? post error log

    – Ganesh Gudghe
    Nov 21 '18 at 8:59













  • Because i actually don't want the row ID. i want a specific code which is in the table according to the merchantName. @pskink

    – nawshin.ahmed
    Nov 21 '18 at 9:00













  • @GaneshGudghe there is no error. its returning the string "error" which i have declared in the code.

    – nawshin.ahmed
    Nov 21 '18 at 9:01













  • Yes. I have tried onItemClickListener. Its getting the selected name. Now i want the specific code of that selected name for which i don't knw what to do. I am now basically want to run a query, get the code, then insert it to a new table.

    – nawshin.ahmed
    Nov 21 '18 at 9:04











  • @VladyslavMatviienko "error" is just a string which i have declared in the fucntion.

    – nawshin.ahmed
    Nov 21 '18 at 9:06














  • 3





    what is the error ? post error log

    – Ganesh Gudghe
    Nov 21 '18 at 8:59













  • Because i actually don't want the row ID. i want a specific code which is in the table according to the merchantName. @pskink

    – nawshin.ahmed
    Nov 21 '18 at 9:00













  • @GaneshGudghe there is no error. its returning the string "error" which i have declared in the code.

    – nawshin.ahmed
    Nov 21 '18 at 9:01













  • Yes. I have tried onItemClickListener. Its getting the selected name. Now i want the specific code of that selected name for which i don't knw what to do. I am now basically want to run a query, get the code, then insert it to a new table.

    – nawshin.ahmed
    Nov 21 '18 at 9:04











  • @VladyslavMatviienko "error" is just a string which i have declared in the fucntion.

    – nawshin.ahmed
    Nov 21 '18 at 9:06








3




3





what is the error ? post error log

– Ganesh Gudghe
Nov 21 '18 at 8:59







what is the error ? post error log

– Ganesh Gudghe
Nov 21 '18 at 8:59















Because i actually don't want the row ID. i want a specific code which is in the table according to the merchantName. @pskink

– nawshin.ahmed
Nov 21 '18 at 9:00







Because i actually don't want the row ID. i want a specific code which is in the table according to the merchantName. @pskink

– nawshin.ahmed
Nov 21 '18 at 9:00















@GaneshGudghe there is no error. its returning the string "error" which i have declared in the code.

– nawshin.ahmed
Nov 21 '18 at 9:01







@GaneshGudghe there is no error. its returning the string "error" which i have declared in the code.

– nawshin.ahmed
Nov 21 '18 at 9:01















Yes. I have tried onItemClickListener. Its getting the selected name. Now i want the specific code of that selected name for which i don't knw what to do. I am now basically want to run a query, get the code, then insert it to a new table.

– nawshin.ahmed
Nov 21 '18 at 9:04





Yes. I have tried onItemClickListener. Its getting the selected name. Now i want the specific code of that selected name for which i don't knw what to do. I am now basically want to run a query, get the code, then insert it to a new table.

– nawshin.ahmed
Nov 21 '18 at 9:04













@VladyslavMatviienko "error" is just a string which i have declared in the fucntion.

– nawshin.ahmed
Nov 21 '18 at 9:06





@VladyslavMatviienko "error" is just a string which i have declared in the fucntion.

– nawshin.ahmed
Nov 21 '18 at 9:06












1 Answer
1






active

oldest

votes


















0














Try this way..



public List<String> getMyItems(String name) {
List<String> stringList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
Cursor c = db.rawQuery(selectQuery, null);
if (c != null) {
c.moveToFirst();
while (c.isAfterLast() == false) {
String name = (c.getString(c.getColumnIndex("Item_Name")));

stringList.add(name);
c.moveToNext();
}
}
return stringList;
}





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%2f53408354%2fhow-to-return-a-string-value-from-sqlite-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














    Try this way..



    public List<String> getMyItems(String name) {
    List<String> stringList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
    Cursor c = db.rawQuery(selectQuery, null);
    if (c != null) {
    c.moveToFirst();
    while (c.isAfterLast() == false) {
    String name = (c.getString(c.getColumnIndex("Item_Name")));

    stringList.add(name);
    c.moveToNext();
    }
    }
    return stringList;
    }





    share|improve this answer






























      0














      Try this way..



      public List<String> getMyItems(String name) {
      List<String> stringList = new ArrayList<>();
      SQLiteDatabase db = this.getReadableDatabase();
      String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
      Cursor c = db.rawQuery(selectQuery, null);
      if (c != null) {
      c.moveToFirst();
      while (c.isAfterLast() == false) {
      String name = (c.getString(c.getColumnIndex("Item_Name")));

      stringList.add(name);
      c.moveToNext();
      }
      }
      return stringList;
      }





      share|improve this answer




























        0












        0








        0







        Try this way..



        public List<String> getMyItems(String name) {
        List<String> stringList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
        Cursor c = db.rawQuery(selectQuery, null);
        if (c != null) {
        c.moveToFirst();
        while (c.isAfterLast() == false) {
        String name = (c.getString(c.getColumnIndex("Item_Name")));

        stringList.add(name);
        c.moveToNext();
        }
        }
        return stringList;
        }





        share|improve this answer















        Try this way..



        public List<String> getMyItems(String name) {
        List<String> stringList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        String selectQuery = "SELECT Item_Name FROM " + USER_TABLE_NAME + " WHERE Item_Name= " + name;
        Cursor c = db.rawQuery(selectQuery, null);
        if (c != null) {
        c.moveToFirst();
        while (c.isAfterLast() == false) {
        String name = (c.getString(c.getColumnIndex("Item_Name")));

        stringList.add(name);
        c.moveToNext();
        }
        }
        return stringList;
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 9:17

























        answered Nov 21 '18 at 9:09









        Android TeamAndroid Team

        7,57811135




        7,57811135






























            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%2f53408354%2fhow-to-return-a-string-value-from-sqlite-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

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

            How to fix TextFormField cause rebuild widget in Flutter