0

I am trying to use an existing database and have realized just now that I have a very simple problem, though the answer may be not so simple:

-App is called myApp -for some reason eclipse calls the package as follows: package com.example.myapp;

-db is called nn.sqlite -db is in assets folder

However, when I try to open it, using a path I thought correct, I get:

09-22 18:48:33.180: I/System.out(838): java.io.FileNotFoundException: /data/data/com.example.myApp/databases/nn

I tried the path with myapp instead of myApp respectively, to no avail.

My path seems to be wrong.

What can I do to handle this, or rather, how to find the correct path?

It was hinted that I should copy my database first - as far as I know, I am already doing this, so here is the code for that:

 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

   }

 }

Is there some other kind of copying which I omitted?

10
  • stackoverflow.com/questions/2605555/… Commented Sep 22, 2014 at 23:07
  • are you saying I should not use a .sqlite file? Commented Sep 22, 2014 at 23:15
  • 1
    I'm saying you have to copy the db from assets to either External memory, or to internal (that data\data path you have tried to use there Commented Sep 22, 2014 at 23:22
  • 1
    If you are doing this (hope you tried to read the most upvoted answer in the link i gave you) show your source code Commented Sep 22, 2014 at 23:27
  • 1
    Mike and I are trying to tell you you have to copy the database(not literaly, but in code, programatically to internal or external storage and then you can open it. The db in assets folder cannot be read directly, and hence you must copy it somewhere else and then try to read/query whatever.. Now google away(Hint: "how to copy db from assets folder android"! Commented Sep 22, 2014 at 23:39

3 Answers 3

1

Try InputStream input = getAssets().open("YourDatabase.sqlite");

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

17 Comments

where should I try that or rather instead of what?
Yea this can let you access an existing DB in the assets folder. If you got no luck in this, Try checking if your creating the database in the correct path (assets folder) if you create the db programmatically...
Well...this is my question, HOW do I find the correct path?
Reading database directly in assets is not possible. You need to store it again in localstorage for reading purposes. I suggest you do something like boolean dbexist = checkdatabase(); so you know if the assets folder got content
Yeah, probably there is a problem in your creation of database if it can't read that. Please give code so I can further understand. Ill try to help I don't have eclipse now
|
0
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");
    }
     }

you dont create the db with this code... There's no sql create... Try working it out with this:

class MyDatabase extends SQLiteOpenHelper {

        public MyDatabase() {
            super(context, DB_NAME, null, DB_VERSION);
        }



        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = String.format("create table %s "
                    + "(%s int primary key, %s text)", 
                    TABLE_NAME,
                    A_IDD, A_FIELD);
            db.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("drop if exist " + TABLE_NAME);
            onCreate(db);
        }

    }
}

4 Comments

Is the idea that I do what you do in your onCreate method for every table? Also, I am not sure how your solution explains my filenotfoundexception. Does ist?
Also, what would A_IDD be?
A_IDD and A_FIELD are two fields in table TABLE_NAME. These are final strings you should declare and initialize in your db class. And YES i am saying you are supposed to make tables. Ofcourse you have file not found exception when YOU HAVENT CREATED THE DB on the location where you are trying to open it. And i would appreciate if you actually try something... This is enough good will from me, newbie
I did try it....that is I tried several things. sqliteOpenAssetshelper did it, if it helps
0

I am not quite sure if I am supposed to answer that here as I got the answer from another question here.

However, I used sqliteOpenAssetHelper. Works perfectly.

Comments

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.