File not found. Exception while trying to open SQLite database for Android

I have the following code as a DataBaseHelper in android android:

public class DbUtils { private static final String DB_PATH = "/data/data/com.project.skcompanion/databases/"; private static final String DB_NAME = "basesh.sqlite"; public static void createDatabaseIfNotExists(Context context) throws IOException { Log.d("check1", "check1"); boolean createDb = false; File dbDir = new File(DB_PATH); File dbFile = new File(DB_PATH + DB_NAME); if (!dbDir.exists()) { dbDir.mkdir(); createDb = true; } else if (!dbFile.exists()) { createDb = true; } else { // Check that we have the latest version of the db boolean doUpgrade = false; if (doUpgrade) { dbFile.delete(); createDb = true; } } Log.d("check2", "check2"); if (createDb) { Log.d("check3", "check3"); // Open your local db AssetManager am = context.getAssets(); InputStream myInput = am.open(DB_NAME); Log.d("check4", "check4"); // Open the empty db OutputStream myOutput = new FileOutputStream(dbFile); // 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(); } } public static SQLiteDatabase getStaticDb() { return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); } 

But when the code tries to open basesh.sqlite, it throws a FileNotFoundException. I have a basesh.sqlite file in assets / databases. After some debugging, I think the problem is in this line.

 InputStream myInput = am.open(DB_NAME); 

But I have no idea why it is not opening my file. Thanks in advance.

+4
source share
3 answers

Your problem is that you put the basesh.sqlite database file in assets/databases , which is wrong. context.getAssets (). open ("basesh.sqlite") looks in assets and finds nothing. So you need to place it under assets .

0
source

Are you trying to open

 InputStream myInput = am.open(DB_NAME); 

Where, as when creating the database, you create it along the way

 private static final String DB_PATH = "/data/data/com.project.skcompanion/databases/"; 

update your code to this

 InputStream myInput = am.open(DB_PATH+DB_NAME); 
0
source

You do not need to create the file yourself. Just create a database and it will be created.

Try this helper:

 public class DatabaseHelper extends SQLiteOpenHelper { private final Context context; public DatabaseHelper(Context context) { super(context, context.getString(R.string.app_name), null, Integer.valueOf(context.getString(R.string.database_version))); this.context = context; } @Override public void onCreate(SQLiteDatabase db) { createDb(db); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String[] versions = context.getResources().getStringArray(R.array.database_upgrade); for (int i = oldVersion; i < newVersion; i++) { String[] statements = versions[i - 1].split(";"); for (String statement : statements) { db.execSQL(statement); } } } private void createDb(SQLiteDatabase db) { try { db.beginTransaction(); String[] tables = context.getResources().getStringArray(R.array.database_tables); for (String table : tables) { db.execSQL(table); } } catch (SQLException e) { Log.e(context.getString(R.string.app_name), context.getString(R.string.database_access_error)); } finally { db.endTransaction(); } } } 

and for resources, including the number of the new version of the database that updates its structure:

 <string name="database_version">2</string> <string-array name="database_tables"> <item>CREATE TABLE "COLLECT" ( _ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, DATE TEXT NOT NULL, SENT INTEGER, OM REAL, CMSR REAL, DC REAL );</item> </string-array> <string-array name="database_upgrade"> <item>ALTER TABLE COLLECT ADD COLUMN SENT INTEGER;</item> </string-array> 

As you can see, when database_version is 1, sqliteHelper just creates my collect table, like my example. If the database already exists, as in the example, and the version number is now 2 or higher, sqliteHelper updates the read array of the db structure database_upgrade , iterating over all its elements.

0
source

All Articles