• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How To Use External SQLite Database inside App???

hi everyone...
I want to ask how we can use an external SQLite inside the app...
I mean i have SQLite database which I've made it with SQLite manager and then I want to use it somehow in my app..


I find the interesting tutorial in
Using your own SQLite database in Android applications | ReignDesign Blog
I follow every step but when I run it, I got force close and I think I made a mistake but probably not...


So, does anyone here have a better solution than this???
Please help me...
 
Hi all
I am new to android,i want connect to external sqlite,insiding assets folder.
Does any sample exist for this to download it?
Can anybody help me?
Thanks a lot.
 
First you need to copy file from assets into app data. Second you need open copied database.
Code:
public static SQLiteDatabase openAssetsDB(Activity a,String filename)
{
    try
    {
        File dbFile = a.getDatabasePath (filename);
        if(!dbFile.exists()) // Copy db just once
        {
            InputStream in = a.getAssets().openInputStream(filename);
            OutputStream out = new FileOutputStream(dbFile);
            copyFile(in,out);
            in.close();
            out.close();
        }
        return SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(),null,0);
    }
    catch(Throwable e)
    {
        e.printStackTrace();
    }
    return null;
}

public static void copyFile(InputStream in, OutputStream out) throws IOException
{
   byte[] buffer = new byte[1024];
   int read;
   while((read = in.read(buffer)) != -1)
    out.write(buffer, 0, read);
}
 
Last edited:
Back
Top Bottom