Newbie here. I received some good info in a previous post I made, and I'm hoping I can get a bit more help :) Thanks for looking.
I am shipping a prepopulated sqlite database with an application. I've gone through the process of copying the database from my assets folder within the source code to the /database/ folder through a routine I found online, that I'm sure you all are familiar with (http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/). This appears to be working correctly. Using DDMS I am able to see the database being copied over and showing up in the proper directory.
Now however, I am getting no results. When I examine logcat, I see the following errors when I attempt to open the DB:
02-19 14:59:40.490: I/Database(4071): sqlite returned: error code = 14, msg = cannot open file at source line 25467
02-19 14:59:40.490: E/Database(4071): sqlite3_open_v2("/data/data/com.testapp.test/databases/pbaMainDb.sqlite", &handle, 2, NULL) failed
EDIT: Here is the ListScreen class I am using to open the database after installation and do a simple query from:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lists);
DbHelper db = new DbHelper(this);
db = new DbHelper(this);
try {
db.createDatabase();
} catch (IOException ioe) {
throw new Error ("Unable to create database");
}
try {
myDbHelper.close();
db.openDataBase();
} catch(SQLException sqle){
throw sqle;
}
}
public Cursor fetchAllTables() {
return myDbHelper.rawQuery("SELECT name FROM sqlite_master WHERE type='table'",
new String[]{});
}
private void fillData() {
//Cursor c = fetchAllTables();
//startManagingCursor(c);
String[] from = new String[] {"name"};
int[] to = new int[] {R.id.listview};
SimpleCursorAdapter notes =
new SimpleCursorAdapter (this, R.layout.list_view, cursor, from, to);
setListAdapter(notes);
}
}
EDIT: Also, from my DbHelper class (where the code is to copy my existing Db), here is the code where I do that (I apologize, it's a bit of a dump):
public class DbHelper extends SQLiteOpenHelper {
// the default android path of the database files it creates
private static String DB_PATH = "/data/data/com.testdata.test/databases/";
private static String DB_NAME = "pbaMainDb.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
// constructor to keep a reference of the passed context in order to access the
public DbHelper (Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
// Create an empty database and then rewrite with original
public void createDatabase() throws IOException {
boolean dbExist = CheckDataBase();
if (dbExist) {
// if exists, do nothing
}else {
//when we call this method the empty database will be created into
this.getReadableDatabase();
try {
copyDataBase();
}
catch (IOException e) {
throw new Error("Error copying database");
}
}
this.close();
}
// Check if the database already exists to avoid re-copy
private boolean CheckDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null SQLiteDatabase.OPEN_READWRITE);
//File dbfile = new File(myPath);
//return dbfile.exists();
} catch (SQLiteException e) {
//database doesn't exist yet
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
// Copies database from assets folder to new empty folder
private void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
//path to just created DB
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream (outFileName);
//transfer bytes from input file to output file
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 {
// open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, 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) {
}
}