Since last few days, I have been working with android and I am very excited about it. This is my first blog on Android and many more is to come.
Normally Sqlite database is used with Android application. So this blog is about how you can use existing Sqlite database in Android application. First you need to add following table in your database. I recommend Sqlite Expert Personal tool to make changes or create new Sqlite database. Open the database and run following query
private static String dbPath= “data/data/com.YourPackageName/applicationDb/”;
private static String dbName = “YourDBName”;
private SQLiteDatabase applicationDatabase;
private final Context applicationContext;
public VocabTesterDatabaseHelper(Context context) {
super(context, dbName , null, 3);
this. applicationContext = context;
}
private boolean checkDataBase(){
File dbFile = new File( dbPath + dbName);
return dbFile.exists();
}
private 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 db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
NObdy cn BEAT U. Cn u do somthing for APPLE IOS. I gt IPHONE 4 s 🙂
It's on the way. Stay tuned.
i have a problem with this, when implementing into my app. a db is created with only the metadata table. do you know how to fix this, i think its a common error. please help
You might have unique id column missing in your other tables.
my db has got 1 table with, _id primary key integer and name text. is this not right? do i need to add anything else special other than metadata?
No I guess that's fine _id is the column which you need in the table. How are you testing that you only have meta data table?
pulled the db file off the data/data/mypackage/database from the emulator
i can send my code?
OK send it on hdave10@gmail.com
How to write a cursor method to display the data of my existing sqlite database table in a list view? Please can i get an entire code for this? I have been trying this for the past few days with no success.
Hi,
I'm interesting in this too.
Thank you
This is also helpful:
http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application
how to retrieve multiple data from existing database in sqlite android eclipse . please send me working project :- vinay@softdew.co.in
Hi Hiren
I am new to Android Programming. How to make sure that that code is working? What is the expected output?
Hi,
I am new to android and i have to retrieve data from the existing database.I used your code and i did everything as it was said here but i have a doubt, already all the tables in my database has a unique primary id ,do i have to change the name of all those primary key as _id. Please reply
Thanks in advance.