mardi 16 juin 2015

How to load pre-existing database (created from Excel) with SQLiteAssetHelper into Android device?

I am trying to load my pre-existing database into my android device.

I used these steps:

  1. Use external library SQLiteAssetHelper available on github
  2. Then according to the documentation: write a wrapper class

    public class DataBaseHelper extends SQLiteAssetHelper {
        private static final String TAG = "DataBaseHelper";
        private static final String DATABASE_NAME = "test.db";
        private static final int DATABASE_VERSION = 1;
    
        public DataBaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        private static final String TABLE_EXAMPLE = "example"; //example is the table name
        private static final String COLUMN_EXAMPLE_A = "A";
    
        public String searchSomething() {
            SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    
            SQLiteDatabase db = getReadableDatabase(); //database name
            //parameters in query method
            String tables = "table"; //
            String[] projectionln = {"test"}; //which columns to return, null return all
            String selection = "row_name = example"; //which rows to return, formatted as WHERE clause excluding WHERE
            String[] selectionArgs = null; //you may include ? in selection which will be filled in by selectionArgs in order
            String groupBy = null; //how to group rows
            String having = null; //filter which row groups to include in cursor
            String sortOrder = null; //how to order the rows
    
            qb.setTables(tables);
            Cursor cursor = qb.query(db, projectionln, selection, selectionArgs, groupBy, having, sortOrder);
            cursor.moveToFirst();
            return cursor.getString(1);
        }
    
    

    }

  3. Instantiate the wrapper class in an Activity to use the database

Like this

public class MainActivity extends AppCompatActivity {
    private DataBaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        db = new DataBaseHelper(this);
        final String name = db.searchSomething();

    }
}

  1. Place your database file into your project under main/src/assets/databases for instance if you database filename is test.db, the file is in main/src/assets/databases/test.db

  2. Add to your database special meta-information like described here

After I have done all this I am left with errors which make me believe that my file format for my database is not yet good. I used a database provided by the library on github and that worked.

06-16 13:38:56.957    9281-9281/com.mycompany.programname E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mycompany.programname, PID: 9281
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mycompany.programname/com.mycompany.programname.mainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference
            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:178)
            at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getReadableDatabase(SQLiteAssetHelper.java:254)
            at com.mycompany.programname.DataBaseHelper.searchSomething(DataBaseHelper.java:75)
            at com.mycompany.programname.HomescreenActivity.onCreate(HomescreenActivity.java:23)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Now my question is, how do you get the database file in the proper sql db format? I think that this must be the problem. Right now I have an excel document which I convert to sql format using the import function from the program DB browser of SQLite. Then I save the database, which then has the sql extension, I searched on internet how to convert it to .db and I found that you could simply change the extension name. But I don't think that's true. Hope someone has any idea or tips because I am stuck at this for over a day.

Aucun commentaire:

Enregistrer un commentaire