0

MyDatabase class

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class Mydatabase extends SQLiteAssetHelper{

    private static final String DATABASE_NAME = "databases/mydb.db";
    private static final int DATABASE_VERSION = 1;

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

    public Cursor getEmployees() {

        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder  qb = new SQLiteQueryBuilder();

        String [] sqlSelect = {"_id", "name", "email"};
        String sqlTables = "contacts";

        qb.setTables(sqlTables);
        Cursor c = qb.query(db, sqlSelect, null, null,
                null, null, null);

        c.moveToFirst();
        return c;

    }

MainActivity class

public class MainActivity extends ListActivity {

    private Cursor employees;
    private MyDatabase db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        db = new MyDatabase(this);
        employees = db.getEmployees(); // you would not typically call this on the main thread

        ListAdapter adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                employees, 
                new String[] {"name"}, 
                new int[] {android.R.id.list});

        getListView().setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        employees.close();
        db.close();
    }

}

I want to connect a database in assets folder I try this example from GitHub but the application is stopped working

the logcate : FATAL EXCEPTION: main java.lang.IllegalStateException: Could not execute method of the activity

      Caused by: java.lang.IllegalArgumentException: File databases/mydb.db contains a path separator

any help please thanks

n-m
  • 37
  • 1
  • 1
  • 10
  • What has "stopped working"? Is an exception being thrown? `text1` is the ID of a `TextView` in the system defined layout [simple_list_item_1](https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_1.xml). The layout is used with `SimpleCursorAdaptor` to create a list of items with the value of DB column `name` displayed for each. – Bob Snyder Jan 05 '16 at 23:06
  • `text1 ` is TextView id in which `name` string will show in every row of ListView – ρяσѕρєя K Jan 06 '16 at 10:20
  • Change the database name to "mydb.db" – Muthukrishnan Rajendran Nov 24 '16 at 01:26

1 Answers1

1

I have just got my database working but I did not reference the database as

   private static final String DATABASE_NAME = "databases/mydb.db";

I access it as follows:

  private static final String DATABASE_NAME = "mydb.db";

I just tested your way and sure enough it breaks it. So remove the database/ and just have mydb.db and it should work.

timv
  • 3,346
  • 4
  • 34
  • 43