How to copy a Sqlitestudio database to the eclipse?

Asked

Viewed 181 times

3

I have a table created by Sqlitestudio that when copied to the folder assets in an app Eclipse doesn’t work.

In case my database class will only have a list( method which is what I need) because the database is already created with the table and 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?

2 answers

1


I solved using a class that copies the bank at the time the application is run, just leave the database file in the directory Assets, that when calling the class it takes the file and copies to the dispotivo, 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();
   } 

}

1

  • I tried by the emulator, In my data/data/package directory does not appear database, do I have to create these database? but I’ve already created I just want to copy

  • What it does is basically this: Checks whether db in the directory /data/data/SEUPACKAGE/Databases already exists. If it doesn’t exist it copies the bytes of the Assets folder where it should. There are 2 classes, one utility (with copy and check methods, etc.) and the other is the example of using this utility class...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.