SqLite table not found

Discussion in 'Application Development' started by mist3r0, Dec 28, 2009.

  1. mist3r0

    mist3r0 Member
    15

    Dec 12, 2009
    19
    0
    15
    Hello boys,

    I've a problem with my application, when I try to connect at DB, this error occur: (Table "prova" not found)

    this is the code for Read to the db:

    Code (Text):
    1.  
    2.  
    3.    public MyDbHelper(Context ctx) {
    4.  
    5.          private SQLiteDatabase db;
    6.  
    7.          try {
    8.                 db = ctx.openOrCreateDatabase("myDB.db", 0, null);
    9.             } catch (SQLiteException e) {
    10.                
    11.                     db = ctx.openOrCreateDatabase("NewMyDB.db", 0, null);
    12.                    
    13.                     db.execSQL("create table prova(_id integer primary key autoincrement)"+"name varchar not null " + "nome2 boolean not null "+"name3 boolean not null "+"name4 boolean not null "+"name5 boolean not null");
    14.  
    15.                     db.close();
    16.           }
    17.  
    :thinking::thinking::thinking::thinking::thinking::thinking::thinking::thinking::thinking:
     

    Advertisement
  2. lekky

    lekky Lover
    VIP Member
    313

    The code in the catch block is only ran when the first statement fails. If it doesn't fail then the catch code isnt run. That means the table prova is never created.

    So what line does that exception occur at? Is is thrown from that code you show or further down when that table is attempting to be accessed?

    It is bad to put code like that in a catch block, catch blocks should only be used to handle an exception, not for logical code.

    Also the db.close(); should be moved outside of that catch block.
     
  3. mist3r0

    mist3r0 Member
    15

    Dec 12, 2009
    19
    0
    15
    Thanks, but I've write the code in this mode because I've thinks taht:

    if in the block "try" the DB isn't found or there are problem to read, in the block "catch" I try to create a DB.

    The problem is strange because I've create a table "prova" with a SqLite grafic tool, there's also an elemente into the DB, Eclipse return me "error table non found" when I try to read a DB, but if I test the DB with grafical tool, the queys are OK! :thinking:

    Can you send me the correct code for read the DB?

    The DB must be put in place specific of Package Space? I've insert it in the root of the package.
     

Share This Page

Loading...