0

I have an android application with a small database. I can use da database with a database manager.

I tried a query and got "no such table", so in order to find out which tables there are, I tried, with help from here:

    Cursor c = helper.getReadableDatabase().rawQuery("SELECT name FROM sqlite_master WHERE type=?",  new String[] {"table"});

    DatabaseUtils.dumpCursor(c);

The result of this, however , is:

 09-22 15:30:59.710: I/System.out(973): >>>>> Dumping cursor   android.database.sqlite.SQLiteCursor@b2d5af30
 09-22 15:30:59.710: I/System.out(973): >>>>> Dumping cursor   android.database.sqlite.SQLiteCursor@b2d5af30
09-22 15:30:59.710: I/System.out(973): 0 {
09-22 15:30:59.720: I/System.out(973):    name=android_metadata
09-22 15:30:59.720: I/System.out(973): }
09-22 15:30:59.720: I/System.out(973): <<<<<

How can I find out what happened to the rest of my database

My dbhelperclass was:

 package com.example.myapp;


public class Helper extends SQLiteOpenHelper

{
private static String path = "/data/data/com.example.myapp/databases/";
private static String db = "nn";
private static String dbpath = path + db;
private SQLiteDatabase myDB;
private  Context con;

 public Helper(Context context) {

     super(context, db, null, 1);
     this.con = context;
     }  

 public Context getContext(){
     return this.con;
 }

 public void createDataBase() throws IOException{


     if(!checkDataBase()){
     this.getReadableDatabase();

     try {

     copyDataBase();

     } catch (IOException e) {

     System.out.println("no Database");

     }
     }

     }


private boolean checkDataBase() {
     SQLiteDatabase checkDB = null;

     try{

     checkDB = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READONLY);

     }catch(SQLiteException e){



     }

     if(checkDB != null){

     checkDB.close();
     return true;

     } else {return false;}


}


private void copyDataBase() throws IOException {

    InputStream myInput = con.getAssets().open(db);  
    OutputStream myOutput = new FileOutputStream(dbpath);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
    }
    myOutput.flush();
    myOutput.close();
    myInput.close();


}


 public void openDataBase() throws SQLException{


    myDB = SQLiteDatabase.openDatabase(dbpath, null, SQLiteDatabase.OPEN_READONLY);

    }

 @Override
 public synchronized void close() {
 if(myDB != null)
 myDB.close();    
 super.close();

 }   


@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}

}

I initialized the helper thus:

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    helper = new Helper(this);

    try {
        helper.createDataBase();
    } catch (IOException ex) {
        System.out.println("no start");
    }
    try {
        helper.openDataBase();
    } catch (SQLException sqlex) {
        System.out.println("does not open");
    }
     }

Ok it turns out I get a filenotfound exception:

 09-22 18:39:04.103: I/System.out(1119): java.io.FileNotFoundException: /data/data/com.example.myapp/databases/nn

However: provided my db is named nn (which it is) and my app is named myapp, should not this be the correct path? nn is in assets.

6
  • 1
    Where and how did you create the table? Btw you can download the SQLite file and use a PC tool to browse it, it's much more convenient. Commented Sep 22, 2014 at 19:58
  • looks like the table isn't there, so you probably didnt create the db or perhaps you tried to open your existing db with a wrong path! Check that path :D Commented Sep 22, 2014 at 20:30
  • The path should be correct. I put the db into assets. I will edit it so that my helper class is seen Commented Sep 22, 2014 at 21:51
  • If I did not create the table, then how come android_metadata is found? Commented Sep 22, 2014 at 21:59
  • 1
    You are not running any 'CREATE TABLE <blah>' scripts so you won't have any tables. The fact you have metadata might be because you have an empty database. Commented Sep 22, 2014 at 22:16

1 Answer 1

1

getReadableDatabase() automatically creates a database, initialized with whatever you create in onCreate() (which is emtpy).

If you want to copy the database from the assets folder, better use a library that is known to work, such as SQLiteAssetHelper.

Sign up to request clarification or add additional context in comments.

3 Comments

ok.which explains why it does not work in general, but does not explain why I get a filenotfound exception.
so that means I have to do it all from scratch or use a library, even though some years ago my code would have worked?
Thank you. Solved more than one Problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.