0

I'm trying to create a login system for an android app using SQLite Database Then I use class DatabaseHelper.java:

private static final String DATABASE_NAME = "test.db";
private final Context context;
SQLiteDatabase db;

private static final String DATABASE_PATH = 
"/data/data/com.example.test2application/databases/";
private final String USER_TABLE = "user";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
    createDb();
}
@Override
public void onCreate(SQLiteDatabase db) {
}

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

public void createDb(){
    boolean dbExist = checkDbExist();

    if(!dbExist){
        this.getReadableDatabase();
        copyDatabase();
    }
}
private boolean checkDbExist(){
    SQLiteDatabase sqLiteDatabase = null;

    try{
        String path = DATABASE_PATH + DATABASE_NAME;
        sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
    } catch (Exception ex){
    }

    if(sqLiteDatabase != null){
        sqLiteDatabase.close();
        return true;
    }
    return false;
}
private void copyDatabase(){
    try {
        InputStream inputStream = context.getAssets().open(DATABASE_NAME);
        String outFileName = DATABASE_PATH + DATABASE_NAME;
        OutputStream outputStream = new FileOutputStream(outFileName);
        byte[] b = new byte[1024];
        int length;
        while ((length = inputStream.read(b)) > 0){
            outputStream.write(b, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
private SQLiteDatabase openDatabase(){
    String path = DATABASE_PATH + DATABASE_NAME;
    db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    return db;
}  
public boolean checkUserExist(String username, String password){
    String[] columns = {"username"};
    db = openDatabase();

    String selection = "username=? and password = ?";
    String[] selectionArgs = {username, password};

    Cursor cursor = db.query(USER_TABLE, columns, selection, selectionArgs, null, null, null);
    int count = cursor.getCount();

    cursor.close();
    close();
}

but my app crushes with error : #################################################################################################################################################### E/SQLiteLog: (26) file is encrypted or is not a database E/DefaultDatabaseErrorHandler: Corruption reported by sqlite on database: /data/data/com.example.test2application/databases/test.db !@ dbObj has been closed E/SQLiteLog: (28) failed to open "/data/data/com.example.test2application/databases/test.db" with flag (131074) and mode_t (0) due to error (2) (28) failed to open "/data/data/com.example.test2application/databases/test.db" with flag (131072) and mode_t (0) due to error (2) (14) cannot open file at line 32546 of [9491ba7d73] (14) os_unix.c:32546: (2) open(/data/data/com.example.test2application/databases/test.db) - E/SQLiteDatabase: Failed to open database '/data/data/com.example.test2application/databases/test.db'. android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 1294): Could not open database ################################################################# Error Code : 1294 (SQLITE_CANTOPEN_ENOENT) Caused By : Specified directory or database file does not exist. (unknown error (code 1294): Could not open database) #################################################################

JIRA12
  • 11
  • 2
  • Hi Jira. Can you please post your code including the part where you create the DB, Also, please see these guidlines: [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve]). Thanks! – Elletlar Jun 30 '20 at 19:56
  • I think the structure of the code is not quite right. You should be extending SQLiteOpenHelper and creating the DB in onCreate and possibly in onUpgrade as well in a simple implementation. See this example [Creating a DB](https://stackoverflow.com/questions/12015731/android-sqlite-example). Regards. – Elletlar Jun 30 '20 at 22:15

0 Answers0