What's wrong with accessing row in database from MainActivity?










0















I'm trying to access a row in my database from MainActivity in the following fashion. I'd like to know a better way to do this since this crashes my app with the error every time. Even after cold rebooting and wiping data.



"23:06 Emulator: glTexImage2D: got err pre :( 0x502 internal 0x1908 format 0x1908 type 0x1401



I assume there is a better way to do this. I'm calling a method which takes a User object which is stored in my database presumably at index 1. (first place in database)



 if(myDb.isNotEmpty() == true) {
MyProgress.calculateBalance***(myDb.getUser(1));***


this is my getUser method.



public User getUser(int id) 
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String String.valueOf(id) , // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit

if (cursor != null)
cursor.moveToFirst();

User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setWeight (Integer.parseInt(cursor.getString(1)));
user.setAge(Integer.parseInt(cursor.getString(2)));

return user;










share|improve this question
























  • this crashes my app is not an error description; please add the relevant stack-trace... besides, calling db.close() is suggested (possibly even the reason for the crash).

    – Martin Zeitler
    Nov 15 '18 at 22:19







  • 1





    that error message is GL drivers related... and has nothing to do with the code. the relevant stack-trace should refer to the MainActivity and have a bunch of lines; commonly displayed in red text.

    – Martin Zeitler
    Nov 15 '18 at 22:33












  • I really can't find other error messages than that. The problem is however solved. It works since I wrapped my getUser method(). Are you saying I should call db.close in MainActivity?

    – Marko Marinkovic
    Nov 15 '18 at 22:41











  • Please note that query() always returns a Cursor or throws an error. So, wrap your code in a try..catch block and forget about if(cursor != null). Also note that if you are returned a valid Cursor object that this does not mean that you have any values. This means that you should be checking the boolean value returned by cursor.moveToFirst() before you proceed with trying to read the values.

    – Barns
    Nov 15 '18 at 22:44
















0















I'm trying to access a row in my database from MainActivity in the following fashion. I'd like to know a better way to do this since this crashes my app with the error every time. Even after cold rebooting and wiping data.



"23:06 Emulator: glTexImage2D: got err pre :( 0x502 internal 0x1908 format 0x1908 type 0x1401



I assume there is a better way to do this. I'm calling a method which takes a User object which is stored in my database presumably at index 1. (first place in database)



 if(myDb.isNotEmpty() == true) {
MyProgress.calculateBalance***(myDb.getUser(1));***


this is my getUser method.



public User getUser(int id) 
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String String.valueOf(id) , // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit

if (cursor != null)
cursor.moveToFirst();

User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setWeight (Integer.parseInt(cursor.getString(1)));
user.setAge(Integer.parseInt(cursor.getString(2)));

return user;










share|improve this question
























  • this crashes my app is not an error description; please add the relevant stack-trace... besides, calling db.close() is suggested (possibly even the reason for the crash).

    – Martin Zeitler
    Nov 15 '18 at 22:19







  • 1





    that error message is GL drivers related... and has nothing to do with the code. the relevant stack-trace should refer to the MainActivity and have a bunch of lines; commonly displayed in red text.

    – Martin Zeitler
    Nov 15 '18 at 22:33












  • I really can't find other error messages than that. The problem is however solved. It works since I wrapped my getUser method(). Are you saying I should call db.close in MainActivity?

    – Marko Marinkovic
    Nov 15 '18 at 22:41











  • Please note that query() always returns a Cursor or throws an error. So, wrap your code in a try..catch block and forget about if(cursor != null). Also note that if you are returned a valid Cursor object that this does not mean that you have any values. This means that you should be checking the boolean value returned by cursor.moveToFirst() before you proceed with trying to read the values.

    – Barns
    Nov 15 '18 at 22:44














0












0








0








I'm trying to access a row in my database from MainActivity in the following fashion. I'd like to know a better way to do this since this crashes my app with the error every time. Even after cold rebooting and wiping data.



"23:06 Emulator: glTexImage2D: got err pre :( 0x502 internal 0x1908 format 0x1908 type 0x1401



I assume there is a better way to do this. I'm calling a method which takes a User object which is stored in my database presumably at index 1. (first place in database)



 if(myDb.isNotEmpty() == true) {
MyProgress.calculateBalance***(myDb.getUser(1));***


this is my getUser method.



public User getUser(int id) 
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String String.valueOf(id) , // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit

if (cursor != null)
cursor.moveToFirst();

User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setWeight (Integer.parseInt(cursor.getString(1)));
user.setAge(Integer.parseInt(cursor.getString(2)));

return user;










share|improve this question
















I'm trying to access a row in my database from MainActivity in the following fashion. I'd like to know a better way to do this since this crashes my app with the error every time. Even after cold rebooting and wiping data.



"23:06 Emulator: glTexImage2D: got err pre :( 0x502 internal 0x1908 format 0x1908 type 0x1401



I assume there is a better way to do this. I'm calling a method which takes a User object which is stored in my database presumably at index 1. (first place in database)



 if(myDb.isNotEmpty() == true) {
MyProgress.calculateBalance***(myDb.getUser(1));***


this is my getUser method.



public User getUser(int id) 
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String String.valueOf(id) , // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit

if (cursor != null)
cursor.moveToFirst();

User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setWeight (Integer.parseInt(cursor.getString(1)));
user.setAge(Integer.parseInt(cursor.getString(2)));

return user;







android sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 22:23







Marko Marinkovic

















asked Nov 15 '18 at 21:34









Marko MarinkovicMarko Marinkovic

378




378












  • this crashes my app is not an error description; please add the relevant stack-trace... besides, calling db.close() is suggested (possibly even the reason for the crash).

    – Martin Zeitler
    Nov 15 '18 at 22:19







  • 1





    that error message is GL drivers related... and has nothing to do with the code. the relevant stack-trace should refer to the MainActivity and have a bunch of lines; commonly displayed in red text.

    – Martin Zeitler
    Nov 15 '18 at 22:33












  • I really can't find other error messages than that. The problem is however solved. It works since I wrapped my getUser method(). Are you saying I should call db.close in MainActivity?

    – Marko Marinkovic
    Nov 15 '18 at 22:41











  • Please note that query() always returns a Cursor or throws an error. So, wrap your code in a try..catch block and forget about if(cursor != null). Also note that if you are returned a valid Cursor object that this does not mean that you have any values. This means that you should be checking the boolean value returned by cursor.moveToFirst() before you proceed with trying to read the values.

    – Barns
    Nov 15 '18 at 22:44


















  • this crashes my app is not an error description; please add the relevant stack-trace... besides, calling db.close() is suggested (possibly even the reason for the crash).

    – Martin Zeitler
    Nov 15 '18 at 22:19







  • 1





    that error message is GL drivers related... and has nothing to do with the code. the relevant stack-trace should refer to the MainActivity and have a bunch of lines; commonly displayed in red text.

    – Martin Zeitler
    Nov 15 '18 at 22:33












  • I really can't find other error messages than that. The problem is however solved. It works since I wrapped my getUser method(). Are you saying I should call db.close in MainActivity?

    – Marko Marinkovic
    Nov 15 '18 at 22:41











  • Please note that query() always returns a Cursor or throws an error. So, wrap your code in a try..catch block and forget about if(cursor != null). Also note that if you are returned a valid Cursor object that this does not mean that you have any values. This means that you should be checking the boolean value returned by cursor.moveToFirst() before you proceed with trying to read the values.

    – Barns
    Nov 15 '18 at 22:44

















this crashes my app is not an error description; please add the relevant stack-trace... besides, calling db.close() is suggested (possibly even the reason for the crash).

– Martin Zeitler
Nov 15 '18 at 22:19






this crashes my app is not an error description; please add the relevant stack-trace... besides, calling db.close() is suggested (possibly even the reason for the crash).

– Martin Zeitler
Nov 15 '18 at 22:19





1




1





that error message is GL drivers related... and has nothing to do with the code. the relevant stack-trace should refer to the MainActivity and have a bunch of lines; commonly displayed in red text.

– Martin Zeitler
Nov 15 '18 at 22:33






that error message is GL drivers related... and has nothing to do with the code. the relevant stack-trace should refer to the MainActivity and have a bunch of lines; commonly displayed in red text.

– Martin Zeitler
Nov 15 '18 at 22:33














I really can't find other error messages than that. The problem is however solved. It works since I wrapped my getUser method(). Are you saying I should call db.close in MainActivity?

– Marko Marinkovic
Nov 15 '18 at 22:41





I really can't find other error messages than that. The problem is however solved. It works since I wrapped my getUser method(). Are you saying I should call db.close in MainActivity?

– Marko Marinkovic
Nov 15 '18 at 22:41













Please note that query() always returns a Cursor or throws an error. So, wrap your code in a try..catch block and forget about if(cursor != null). Also note that if you are returned a valid Cursor object that this does not mean that you have any values. This means that you should be checking the boolean value returned by cursor.moveToFirst() before you proceed with trying to read the values.

– Barns
Nov 15 '18 at 22:44






Please note that query() always returns a Cursor or throws an error. So, wrap your code in a try..catch block and forget about if(cursor != null). Also note that if you are returned a valid Cursor object that this does not mean that you have any values. This means that you should be checking the boolean value returned by cursor.moveToFirst() before you proceed with trying to read the values.

– Barns
Nov 15 '18 at 22:44













2 Answers
2






active

oldest

votes


















0














The last part of your method should look like this:



if ((cursor != null) && (cursor.moveToFirst()) 
User user = new User();
user.setId(Integer.parseInt(cursor.getString(0)));
user.setWeight (Integer.parseInt(cursor.getString(1)));
user.setAge(Integer.parseInt(cursor.getString(2)));
return user;
else
return null;



This code ensures that the if the cursor returned is null or does not contain at least 1 row, it will not throw an error.

In this case the method will return null.

Of course if the cursor is prepared with no errors and myDB is properly initialized there is no reason for the cursor to be null, so you must check for valid column, table and database names.

Also after every change you make to any of these always uninstall the app from the emulator/device so the database is deleted and rerun the app to recreate everything.






share|improve this answer
































    1














    Better way would be use Save data in a local database using Room



    Here is your code I see null check is done but it protect only one statement from NPE. You need to wrap user creation under this if condition.



    @Nullable
    public User getUser(int id)
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, // a. table
    COLUMNS, // b. column names
    " id = ?", // c. selections
    new String String.valueOf(id) , // d. selections args
    null, // e. group by
    null, // f. having
    null, // g. order by
    null); // h. limit

    if (cursor != null && cursor.moveToFirst())
    User user = new User();
    user.setId(Integer.parseInt(cursor.getString(0)));
    user.setWeight (Integer.parseInt(cursor.getString(1)));
    user.setAge(Integer.parseInt(cursor.getString(2)));

    return user;


    return null;






    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%2f53328227%2fwhats-wrong-with-accessing-row-in-database-from-mainactivity%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      The last part of your method should look like this:



      if ((cursor != null) && (cursor.moveToFirst()) 
      User user = new User();
      user.setId(Integer.parseInt(cursor.getString(0)));
      user.setWeight (Integer.parseInt(cursor.getString(1)));
      user.setAge(Integer.parseInt(cursor.getString(2)));
      return user;
      else
      return null;



      This code ensures that the if the cursor returned is null or does not contain at least 1 row, it will not throw an error.

      In this case the method will return null.

      Of course if the cursor is prepared with no errors and myDB is properly initialized there is no reason for the cursor to be null, so you must check for valid column, table and database names.

      Also after every change you make to any of these always uninstall the app from the emulator/device so the database is deleted and rerun the app to recreate everything.






      share|improve this answer





























        0














        The last part of your method should look like this:



        if ((cursor != null) && (cursor.moveToFirst()) 
        User user = new User();
        user.setId(Integer.parseInt(cursor.getString(0)));
        user.setWeight (Integer.parseInt(cursor.getString(1)));
        user.setAge(Integer.parseInt(cursor.getString(2)));
        return user;
        else
        return null;



        This code ensures that the if the cursor returned is null or does not contain at least 1 row, it will not throw an error.

        In this case the method will return null.

        Of course if the cursor is prepared with no errors and myDB is properly initialized there is no reason for the cursor to be null, so you must check for valid column, table and database names.

        Also after every change you make to any of these always uninstall the app from the emulator/device so the database is deleted and rerun the app to recreate everything.






        share|improve this answer



























          0












          0








          0







          The last part of your method should look like this:



          if ((cursor != null) && (cursor.moveToFirst()) 
          User user = new User();
          user.setId(Integer.parseInt(cursor.getString(0)));
          user.setWeight (Integer.parseInt(cursor.getString(1)));
          user.setAge(Integer.parseInt(cursor.getString(2)));
          return user;
          else
          return null;



          This code ensures that the if the cursor returned is null or does not contain at least 1 row, it will not throw an error.

          In this case the method will return null.

          Of course if the cursor is prepared with no errors and myDB is properly initialized there is no reason for the cursor to be null, so you must check for valid column, table and database names.

          Also after every change you make to any of these always uninstall the app from the emulator/device so the database is deleted and rerun the app to recreate everything.






          share|improve this answer















          The last part of your method should look like this:



          if ((cursor != null) && (cursor.moveToFirst()) 
          User user = new User();
          user.setId(Integer.parseInt(cursor.getString(0)));
          user.setWeight (Integer.parseInt(cursor.getString(1)));
          user.setAge(Integer.parseInt(cursor.getString(2)));
          return user;
          else
          return null;



          This code ensures that the if the cursor returned is null or does not contain at least 1 row, it will not throw an error.

          In this case the method will return null.

          Of course if the cursor is prepared with no errors and myDB is properly initialized there is no reason for the cursor to be null, so you must check for valid column, table and database names.

          Also after every change you make to any of these always uninstall the app from the emulator/device so the database is deleted and rerun the app to recreate everything.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 15 '18 at 21:58

























          answered Nov 15 '18 at 21:48









          forpasforpas

          16.4k3627




          16.4k3627























              1














              Better way would be use Save data in a local database using Room



              Here is your code I see null check is done but it protect only one statement from NPE. You need to wrap user creation under this if condition.



              @Nullable
              public User getUser(int id)
              SQLiteDatabase db = this.getReadableDatabase();
              Cursor cursor = db.query(TABLE_NAME, // a. table
              COLUMNS, // b. column names
              " id = ?", // c. selections
              new String String.valueOf(id) , // d. selections args
              null, // e. group by
              null, // f. having
              null, // g. order by
              null); // h. limit

              if (cursor != null && cursor.moveToFirst())
              User user = new User();
              user.setId(Integer.parseInt(cursor.getString(0)));
              user.setWeight (Integer.parseInt(cursor.getString(1)));
              user.setAge(Integer.parseInt(cursor.getString(2)));

              return user;


              return null;






              share|improve this answer





























                1














                Better way would be use Save data in a local database using Room



                Here is your code I see null check is done but it protect only one statement from NPE. You need to wrap user creation under this if condition.



                @Nullable
                public User getUser(int id)
                SQLiteDatabase db = this.getReadableDatabase();
                Cursor cursor = db.query(TABLE_NAME, // a. table
                COLUMNS, // b. column names
                " id = ?", // c. selections
                new String String.valueOf(id) , // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit

                if (cursor != null && cursor.moveToFirst())
                User user = new User();
                user.setId(Integer.parseInt(cursor.getString(0)));
                user.setWeight (Integer.parseInt(cursor.getString(1)));
                user.setAge(Integer.parseInt(cursor.getString(2)));

                return user;


                return null;






                share|improve this answer



























                  1












                  1








                  1







                  Better way would be use Save data in a local database using Room



                  Here is your code I see null check is done but it protect only one statement from NPE. You need to wrap user creation under this if condition.



                  @Nullable
                  public User getUser(int id)
                  SQLiteDatabase db = this.getReadableDatabase();
                  Cursor cursor = db.query(TABLE_NAME, // a. table
                  COLUMNS, // b. column names
                  " id = ?", // c. selections
                  new String String.valueOf(id) , // d. selections args
                  null, // e. group by
                  null, // f. having
                  null, // g. order by
                  null); // h. limit

                  if (cursor != null && cursor.moveToFirst())
                  User user = new User();
                  user.setId(Integer.parseInt(cursor.getString(0)));
                  user.setWeight (Integer.parseInt(cursor.getString(1)));
                  user.setAge(Integer.parseInt(cursor.getString(2)));

                  return user;


                  return null;






                  share|improve this answer















                  Better way would be use Save data in a local database using Room



                  Here is your code I see null check is done but it protect only one statement from NPE. You need to wrap user creation under this if condition.



                  @Nullable
                  public User getUser(int id)
                  SQLiteDatabase db = this.getReadableDatabase();
                  Cursor cursor = db.query(TABLE_NAME, // a. table
                  COLUMNS, // b. column names
                  " id = ?", // c. selections
                  new String String.valueOf(id) , // d. selections args
                  null, // e. group by
                  null, // f. having
                  null, // g. order by
                  null); // h. limit

                  if (cursor != null && cursor.moveToFirst())
                  User user = new User();
                  user.setId(Integer.parseInt(cursor.getString(0)));
                  user.setWeight (Integer.parseInt(cursor.getString(1)));
                  user.setAge(Integer.parseInt(cursor.getString(2)));

                  return user;


                  return null;







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 '18 at 23:40

























                  answered Nov 15 '18 at 22:11









                  PrakashPrakash

                  2,9402130




                  2,9402130



























                      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%2f53328227%2fwhats-wrong-with-accessing-row-in-database-from-mainactivity%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

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      ReactJS Fetched API data displays live - need Data displayed static

                      政党