Ashish Wadatkar
Ashish Wadatkar

Reputation: 427

How to read preexisting database into Android using ORMLite

I want to use preexisting database file using ORMLite in android. I have database.db file of already creted database. I want to use it in my app.

My class extends OrmLiteSqliteOpenHelper.

Can any one have an idea? Please help

I use to copy database file into data path using
public static void copyDataBase(String path,Context c) throws IOException{

    //Open your local db as the input stream
    InputStream myInput = c.getAssets().open("Mydb.db");

    // Path to the just created empty db
    String outFileName = "/data/data/packageName/databases/databaseName";
    String outFileName2 = "/data/data/packageName/databases/";
    File file = new File(outFileName2);
    if(!file.exists())
        file.mkdirs();
    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close(); 
}

But it wont help me .

Upvotes: 0

Views: 1118

Answers (2)

Ashish Wadatkar
Ashish Wadatkar

Reputation: 427

I have solved the problem by below implementation

try {

            String destPath = "/data/data/" + context.getPackageName()
                    + "/databases";
            Log.v("LOG", destPath);

            File f = new File(destPath);
            if (!f.exists()) {
                f.mkdirs();
                File outputFile = new File("/data/data/"
                        + context.getPackageName() + "/databases",
                        "Name ofyour Database");
                outputFile.createNewFile();

                String DatabaseFile = "database file name from asset folder";

                InputStream in = context.getAssets().open(DatabaseFile);
                OutputStream out = new FileOutputStream(outputFile);

                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
                in.close();
                out.close();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            Log.v("TAG", "ioexeption");
            e.printStackTrace();
        }

Upvotes: 0

Qben
Qben

Reputation: 2623

This is kind of a stab in the dark since I do not know what your exact problem is, but can't you just specify the database file (and path) in the OrmLiteSqliteOpenHelper constructor?

OrmLiteSqliteOpenHelper(android.content.Context context, String databaseName, android.database.sqlite.SQLiteDatabase.CursorFactory factory, int databaseVersion)

Also there are a number of questions on this forum that deal with opening custom database files. This question is not ORMLite specific but they might get you forward since it open a custom database file.

Hope this help you a bit on your way.

Upvotes: 0

Related Questions