How to copy a database from SQLiteStudio to eclipse?

3

I have a table created from SQLiteStudio that when copied to the assets folder in an Eclipse application does not work.

In case my bank class will only have one method list (which is what I need) because the bank is already created with the table and the data.

 public class DatabaseHandler extends SQLiteOpenHelper  {

     private static final int DATABASE_VERSION = 1;  
        private static final String DATABASE_NAME = "rtw";  




        public DatabaseHandler(Context context) {  
            super(context, DATABASE_NAME, null, DATABASE_VERSION);  
        }  

    @Override
    public void onCreate(SQLiteDatabase db) {


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


      public List<String> getAllLabels(){  
            List<String> list = new ArrayList<String>();  

            // Select All Query  
            String selectQuery = "SELECT  * FROM regiao where uf = 'RS' " ; 

            SQLiteDatabase db = this.getReadableDatabase();  
            Cursor cursor = db.rawQuery(selectQuery, null);//selectQuery,selectedArguments  

            // looping through all rows and adding to list  
            if (cursor.moveToFirst()) {  
                do {  
                    list.add(cursor.getString(1));//adding 2nd column data  
                } while (cursor.moveToNext());  
            }  
            // closing connection  
            cursor.close();  
            db.close();  

            // returning lables  
            return list;  
        }

How to solve the problem?

    
asked by anonymous 17.12.2014 / 06:48

2 answers

1

I solved using a class that makes the bank copy at the time the application is run, just leave the bank file in the assets directory, which when calling the class it takes the file and copies it to the device, see:

 */
public class BdCopy {


 public static void copiaBanco(Context ctx, String nomeDB){

   // Cria o banco vazio
   SQLiteDatabase db = ctx.openOrCreateDatabase(
     nomeDB, Context.MODE_WORLD_WRITEABLE, null);

   db.close();

   try {
     // Abre o arquivo que deve estar na pasta assets
     InputStream is = ctx.getAssets().open(nomeDB);
     // Abre o arquivo do banco vazio ele fica em:
     // /data/data/nome.do.pacote.da.app/databases
     FileOutputStream fos = new FileOutputStream(
       ctx.getDatabasePath(nomeDB));

     // Copia byte a byte o arquivo do assets para
     // o aparelho/emulador

     byte[] buffer = new byte[1024];
     int read;
     while ((read = is.read(buffer)) > 0){
       fos.write(buffer, 0, read);
     }
   } catch (IOException e) {
     e.printStackTrace();
   } 

}

    
20.01.2015 / 19:28
1

You basically have to copy to the assets folder and then move to the /data/data/SEUPACOTE/databases directory.

Here's a link that will help you, even though it's in English , it's easy to understand.

Using your SQLite database on Android apps Edit:

There is also a lib that facilitates the process for you here:

Android SQLITE Asset Helper via GitHub

    
17.12.2014 / 07:59