1. Download our Official Android App: Forums for Android!

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

Discussion in 'Android Development' started by Rommy Saiko, May 11, 2011.

  1. Rommy Saiko

    Rommy Saiko Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    May 11, 2011

    May 11, 2011
    1
    0
    5
    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...
     

    Advertisement

  2. afshinkhani

    afshinkhani Lurker
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    May 5, 2015

    May 5, 2015
    1
    0
    5
    Male
    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.
     
  3. jbak

    jbak Member
    Rank:
    None
    Points:
    46
    Posts:
    43
    Joined:
    Dec 16, 2013

    Dec 16, 2013
    43
    9
    46
    Male
    Ukraine, Odessa
    First you need to copy file from assets into app data. Second you need open copied database.
    Code (Text):
    1. public static SQLiteDatabase openAssetsDB(Activity a,String filename)
    2. {
    3.     try
    4.     {
    5.         File dbFile = a.getDatabasePath (filename);
    6.         if(!dbFile.exists()) // Copy db just once
    7.         {
    8.             InputStream in = a.getAssets().openInputStream(filename);
    9.             OutputStream out = new FileOutputStream(dbFile);
    10.             copyFile(in,out);
    11.             in.close();
    12.             out.close();
    13.         }
    14.         return SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(),null,0);
    15.     }
    16.     catch(Throwable e)
    17.     {
    18.         e.printStackTrace();
    19.     }
    20.     return null;
    21. }
    22.  
    23. public static void copyFile(InputStream in, OutputStream out) throws IOException
    24. {
    25.    byte[] buffer = new byte[1024];
    26.    int read;
    27.    while((read = in.read(buffer)) != -1)
    28.     out.write(buffer, 0, read);
    29. }
    30.  
     
    #3 jbak, May 7, 2015
    Last edited: May 7, 2015
    afshinkhani likes this.

Share This Page

Loading...