android can't open sqlite database from assets folder

2.7k Views Asked by At

i have one .sqllite database file and i try to open this database and insert some values.but when i run application i have log cat error.can't open database this is a my code

public class IrocDBController extends SQLiteOpenHelper {

String DB_PATH = null;
private static String DB_NAME = "CredoDatabase.sqlite";
private static String usm_Users = "Usm_Users";

private SQLiteDatabase myDataBase;

private final Context myContext;
public IrocDBController(Context context) {

    super(context, DB_NAME, null, 2);
    this.myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void InsertToLoginTable(String username, String password,
        String LoanOfficerId, String BranchId) {
    ContentValues newValues = new ContentValues();
    newValues.put("UserName", username);
    newValues.put("Password", password);
    newValues.put("LoanOfficerId", LoanOfficerId);
    newValues.put("BranchId", BranchId);

    myDataBase.insert(usm_Users, null, newValues);
}


public boolean DublicateValues(int LoanOfficerId) {
    myDataBase = this.getWritableDatabase();
    Cursor cursor = myDataBase.rawQuery("select * from " + usm_Users + " where "
            + "LoanOfficerId" + " == " + LoanOfficerId, null);
    boolean exists = (cursor.getCount() > 0);
    Log.e("aaaaaaaaaaaaaaaaaaaa", String.valueOf(cursor.getCount()));
    cursor.close();
    return exists;
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        // do nothing - database already exist
    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");

        }
    }

}

public String getNameFildeEntry(String username) {
    Cursor cursor = myDataBase.query(usm_Users, null, " UserName=?",
            new String[] { username }, null, null, null);
    if (cursor.getCount() < 1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String getUserName = cursor
            .getString(cursor.getColumnIndex("UserName"));

    cursor.close();
    return getUserName;
}

public String getPasswordFildeEntry(String Password) {
    Cursor cursor = myDataBase.query(usm_Users, null, " Password=?",
            new String[] { Password }, null, null, null);
    if (cursor.getCount() < 1) // UserName Not Exist
    {
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String getUserPassword = cursor.getString(cursor
            .getColumnIndex("Password"));

    cursor.close();
    return getUserPassword;
}


private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}


private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream myOutput = new FileOutputStream(outFileName);
    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 {


    String path = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(path, null,
            SQLiteDatabase.OPEN_READWRITE);
}

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



@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

i use nativec tu create sqlite database and in my database i created some tables and etc and this database i inserted in assets folder i have no idea what am i doing wrong. if anyone knows solution please help me this is a my log cat error code enter image description here

2

There are 2 best solutions below

0
On

It could be because the sql file is in your asset folder. I had a similar issue using an exe which was in my asset folder. I had to copy it to the phone storage when I wanted to use it, then delete it when I knew I wouldn't use it anymore. It may be caused by the fact that the application on the phone is an .apk file (which is just like a zip file) and that you can't acces files inside it unless you use the adroid assets helper.

0
On

This has been discussed many times already. See Reading sqlite file from asset folder for example or use the search function :-)