Possible Duplicate:
How to access an existing sqlite database in Android?
I've been searching for a long time but I can't find the answer. I want to copy my existing Sqlite database test.db with data in it in my application. So when the users download my app from the application the db is delivered with it.
Now I'v seen a lot of references to the tutorials this and this. But none of them work for me.
public class DatabaseAdapter extends SQLiteOpenHelper {
private static String dbPath= "data/data/test.test.test/databases/";
private static String dbName = "test";
private SQLiteDatabase applicationDatabase;
private final Context applicationContext;
public DatabaseAdapter(Context context) {
super(context, dbName , null, 3);
this. applicationContext = context;
}
public boolean checkDataBase(){
File dbFile = new File( dbPath + dbName);
return dbFile.exists();
}
public void copyDataBase() throws IOException{
try {
InputStream input = applicationContext .getAssets().open(dbName);
String outPutFileName= dbPath + dbName ;
OutputStream output = new FileOutputStream( outPutFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer))>0){
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
}
catch (IOException e) {
Log.v("error",e.toString());
}
}
public void openDataBase() throws SQLException{
String fullDbPath= dbPath + dbName;
applicationDatabase = SQLiteDatabase.openDatabase( fullDbPath, null,SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if( applicationDatabase != null)
applicationDatabase .close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void getit(){
this.getReadableDatabase().rawQuery("SELECT * FROM test", null);
}
}
I get the error code = 1 no such table. If I check the database is created but only with the table android_metadata.
Someone a working example?