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?

newnewbie
  • 993
  • 2
  • 11
  • 26
  • http://stackoverflow.com/questions/2605555/android-accessing-assets-folder-sqlite-database-file-with-sqlite-extension – Marija Milosevic Sep 22 '14 at 23:07
  • are you saying I should not use a .sqlite file? – newnewbie Sep 22 '14 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 – Marija Milosevic Sep 22 '14 at 23:22
  • Yes, I am doing that and always get a file not found exception, hence I guess my path must be wrong. But how to find the correct path? – newnewbie Sep 22 '14 at 23:25
  • 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 – Marija Milosevic Sep 22 '14 at 23:27
  • I did read it my source code looks almost exactly the same I guess we uses the same tutorial......wait a minute just commented sth out to test something. However, find my sourcecode here: http://stackoverflow.com/questions/25981883/android-sqlite-database-nothing-there-except-android-metadata?noredirect=1#comment40688804_25981883 – newnewbie Sep 22 '14 at 23:31
  • 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"! – Marija Milosevic Sep 22 '14 at 23:39
  • But what is written in evry answer I find when I google that I have already done! Have you not read my copydatabase method? – newnewbie Sep 22 '14 at 23:41
  • I am literally doing the same thing in my code you are telling my to to. It is almost verbosely the same thing! What am I missing? – newnewbie Sep 22 '14 at 23:48
  • Look I checked I do not know how many times. In my dbHelper, which I posted above, I first check if a db is there, if not I create an empty one, then I copy my db into it. Where is the difference between this and what you are advising? – newnewbie Sep 23 '14 at 00:36

3 Answers3

1

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

Mike
  • 1,313
  • 12
  • 21
  • where should I try that or rather instead of what? – newnewbie Sep 22 '14 at 23:06
  • 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... – Mike Sep 22 '14 at 23:07
  • Well...this is my question, HOW do I find the correct path? – newnewbie Sep 22 '14 at 23:09
  • And no, I tried your idea, but the file STILL is not found. – newnewbie Sep 22 '14 at 23:14
  • If you stored it here /data/data/" + your package + "/databases/ then retrieve it from there as well. You need your context for using getAssets() as well. – Mike Sep 22 '14 at 23:14
  • This is just sample code, I currently don't have eclipse: InputStream mInput = mContext.getAssets().open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream mOutput = new FileOutputStream(outFileName); – Mike Sep 22 '14 at 23:15
  • What do you mean stored it there? I stored it in assets, as is said in about every tutorial I could find. And I thought retrieving it from exactly there was what I was doing in my code? – newnewbie Sep 22 '14 at 23:15
  • where DB_PATH and DB_NAME is a private static string. – Mike Sep 22 '14 at 23:16
  • 2
    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 – Mike Sep 22 '14 at 23:19
  • but....what you are suggestting in your comment from 3 minutes ago was what I was doing was not it? How does one store it in localstorage? – newnewbie Sep 22 '14 at 23:20
  • Look I am already doing what you suggested. The Problem is that my version of mContect.getAssets().open(DB_Name) gives a filenotfoundexception – newnewbie Sep 22 '14 at 23:26
  • 1
    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 – Mike Sep 22 '14 at 23:33
  • Find my code here....http://stackoverflow.com/questions/25981883/android-sqlite-database-nothing-there-except-android-metadata?noredirect=1#comment40688804_25981883 – newnewbie Sep 22 '14 at 23:34
  • You haven't created a table mate. CREATE TABLE tablename (properties) etc. It cannot read sqlite files like that – Mike Sep 22 '14 at 23:39
  • also do DROP TABLE IF EXIST for checking if it alrady created table. I'll be off in a few. – Mike Sep 22 '14 at 23:40
  • I used this tutorial: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ there is nothing about creating tables here....are they wrong? – newnewbie Sep 22 '14 at 23:46
  • That code is probably outdated or maybe deprecated since it was built in 2009. People do the creating / dropping of table in eclipse itself nowadays – Mike Sep 23 '14 at 01:19
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);
        }

    }
}
Marija Milosevic
  • 516
  • 4
  • 16
  • 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? – newnewbie Sep 23 '14 at 16:30
  • Also, what would A_IDD be? – newnewbie Sep 23 '14 at 16:53
  • 1
    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 – Marija Milosevic Sep 23 '14 at 17:36
  • I did try it....that is I tried several things. sqliteOpenAssetshelper did it, if it helps – newnewbie Sep 23 '14 at 17:37
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.

newnewbie
  • 993
  • 2
  • 11
  • 26