-1

I have a huge Database. And i want to save it in the Assets folder and read the data from it.

How i can do this?. I read many answers but i didn't understand how to do it.

Please, Help me with the full steps. Because i am A beginner

Héctor
  • 24,444
  • 35
  • 132
  • 243
Besho
  • 3
  • 5
  • Not work with me !! I tried but it's always make Errors ! – Besho Jan 07 '16 at 08:26
  • Can you specify the errors? – Héctor Jan 07 '16 at 08:29
  • I only want to know the full steps. 1 by 1. I want to know where to put these codes. And i want to connect My DatabaseHelper class to the File.sql that it's in the Assets folder – Besho Jan 07 '16 at 09:08
  • Stack Overflow is not a code writing service. What you can ask is a specific question about your actual code. – CL. Jan 07 '16 at 09:23

1 Answers1

1

Save your database in

assets/databases/database_name.sqlite

Add this to your build.gradle

compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'

and sync your app.

Then Create database class:

MyDatabase.java

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class MyDatabase extends SQLiteAssetHelper {

   private static final String DATABASE_NAME = "database_name.sqlite";
   private static final int DATABASE_VERSION = 1;

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

Then access your database like this where you want:

 MyDatabase myDatabase = new MyDatabase(context);
 SQLiteDatabase db = myDatabase.getReadableDatabase();
Asad Haider
  • 504
  • 1
  • 5
  • 17
  • add : compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' to build.gradle(project: NAME) ?? or to build.gradle(Module:app??) – Besho Jan 07 '16 at 09:34
  • Its giving me this error: Gradle DSL method not found: 'compile()' – Besho Jan 07 '16 at 09:38
  • Add in **build.gradle(Module:app)** like this: `dependencies { compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' }` – Asad Haider Jan 07 '16 at 09:39
  • ok, Now there is no any error. But, How i will select the data ? txt.setText(); I want to show the data on the TextView. – Besho Jan 07 '16 at 09:52
  • `String selectQuery = "SELECT * FROM " + TABLE_NAME; Cursor cursor= db.rawQuery(selectQuery, null); if (cursor.moveToFirst()) { do { // get your data here i.e String column1 = cursor.getString(0); } }while (cursor.moveToNext()); }` – Asad Haider Jan 07 '16 at 10:10
  • Still have Errors. Can you connect to my PC via TeamViewer Please ? – Besho Jan 07 '16 at 10:28
  • here is the link for my new post: http://stackoverflow.com/questions/34653230/selecting-data-from-sqlite-database-located-in-assets-folder – Besho Jan 07 '16 at 10:50