Reputation: 2172
My code is now as follows:
Main app java:
String TAG = "DATABASES";
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/data.db";
File f = new File(destPath);
if(!f.exists()){
Log.v(TAG,"Dest DB doesn't exist");
InputStream in = getAssets().open("airports.db");
OutputStream out = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
} else {
Log.v(TAG,"File exists: " + destPath);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v(TAG,"ioexeption");
e.printStackTrace();
}
DBManager dbManager = new DBManager(this);
Log.v(TAG,"Database is there with version: "+dbManager.getReadableDatabase().getVersion());
//String sql = "select * from airports where IATA='GRO' ";
String sql = "select count(*) from airports";
SQLiteDatabase db = dbManager.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
Log.v(TAG,"Query Result:"+cursor);
cursor.close();
db.close();
dbManager.close();
My DBManager.java:
package com.jammo.mywidget4;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBManager extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DATABASES";
public DBManager(Context context) {
super(context, "data.db", null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v(TAG,"On create Called:"+db.getPath());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Now the execution runs ok in the main.java on initiation of "db", however fails on the next line where it tries to execute rawQuery()
FYI "select count(*) from airports" run on my Sqlite DB Manager GUI returns 1650 rows.
Error log is saying :
03-04 21:54:24.200: E/AndroidRuntime(11513): FATAL EXCEPTION: main
03-04 21:54:24.200: E/AndroidRuntime(11513): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jammo.mywidget4/com.jammo.mywidget4.SectorInfo}: android.database.sqlite.SQLiteException: no such table: airports: , while compiling: select count(*) from airports
Local /assets/airports.db "seems" to be being detected and copied to /data/data/mypackage/databases/data.db as "file is found"
Many thanks J
Upvotes: 1
Views: 433
Reputation: 48871
Try it without using your DBManager class.
Comment out this line...
DBManager dbManager = new DBManager(this);
...then open the database explicitly by replacing this line...
SQLiteDatabase db = dbManager.getReadableDatabase();
...with...
SQLiteDatabase db = SQLiteDatabase.openDatabase(destPath, null, SQLiteDatabase.OPEN_READONLY);
Then clear the app data so the database is re-copied from your assets
directory.
EDIT: Even though my suggestion worked, it's actually just a workaround and the root of your problem was this...
String destPath = "/data/data/" + getPackageName() + "/databases/data.db";
You should avoid using hard-coded paths with Android - they may work fine for many devices but there is no guarantee they'll work with all devices or even with all versions of Android.
The problem was you were creating / copying your database to that path but the DBManager class was presumably creating an empty database located somewhere on a different path in the file-system.
To fix this for all devices and Android versions, before copying the database, I would use...
SQLiteDatabase.openOrCreateDatabase("data.db", null)
...to create a 'dummy' (empty) database. Then I'd call...
getDatabasePath("data.db")
...to get the absolute path to data.db
. You can then overwrite that database during the copy operation and from then on you can use a class which extends SQLiteOpenHelper
and it will find the correct database.
Upvotes: 2