-1

I have created a sqlite database and copied the .db file to assets folder of the application as stated in question 2605555. The code compile fine. During runtime, it says unable to read the file.

I would like to use readymade .db file and want to create database only if it does not exist.

Need help to create the database using .db file.

Suresh PB
  • 169
  • 1
  • 2
  • 16

2 Answers2

0
 @Override
        public void onCreate(SQLiteDatabase db) {
    File file=new File("YOUR DBfile PATH");
    db.openOrCreateDatabase(file,null,CREATE_IF_NECESSARY);
    }
Akash kv
  • 429
  • 4
  • 13
0
package com.airi.buyue.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import com.airi.buyue.BuyueApp;
import com.airi.buyue.R;
import com.airi.buyue.util.FileUtils;
import com.airi.buyue.util.Utilities;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class DataManager {

    public static final String DB_NAME = "zonen.db";
    public static final String TABLE_NAME = "m_nzone_copy";

    private SQLiteDatabase database;
    private final Context context;

    public DataManager(Context context) {
        this.context = context;
        getPath();
    }

    public SQLiteDatabase getDatabase() {
        return database;
    }

    public void setDatabase(SQLiteDatabase database) {
        this.database = database;
    }

    public void openDatabase() {
        this.database = this.openDatabase(getPath() + "/" + DB_NAME);
    }

    public  String getPath(){
        String DATABASE_PATH;
        if (!Utilities.isSdcardExisting()) {
            DATABASE_PATH = BuyueApp.get().getFilesDir().getPath();
        }else{
            DATABASE_PATH = Environment.getExternalStorageDirectory().getPath();
        }
        return DATABASE_PATH;
    }

    public boolean needCreate() {
        String dbfile = getPath() + "/" + DB_NAME;
        try {
            if (!(new File(dbfile).exists())) {
                return true;
            }
            FileUtils.updateFile(dbfile);
            return false;

        }  catch (Throwable ignored) {
        }
        return true;
    }

    private SQLiteDatabase openDatabase(String dbfile) {
        try {
            if (!(new File(dbfile).exists())) {
                InputStream is = this.context.getResources().openRawResource(R.raw.zone);
                FileOutputStream fos = new FileOutputStream(dbfile);
                int BUFFER_SIZE = 400000;
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }
            FileUtils.updateFile(dbfile);
            return SQLiteDatabase.openOrCreateDatabase(dbfile,null);

        } catch (FileNotFoundException ignored) {
        } catch (IOException ignored) {
        } catch (Throwable ignored) {
        }
        return null;
    }

    public void closeDatabase() {
        if(this.database!=null){
            this.database.close();
        }
    }
}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42