0

I have created this class in my android application. But when I'm going to retrieve data from created database, it's showing the database not available. Please help me to do this. Thanks

public class DatabaseHandler extends SQLiteOpenHelper {

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

      private static final String TABLE_NAME = "numbers";

    public DatabaseHandler(Context context, String name, CursorFactory factory,
            int version) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create database query
        db.execSQL("create table " + TABLE_NAME + " (district TEXT, category TEXT, description TEXT, number TEXT); ");

        //Insert query
        db.execSQL("insert into " + TABLE_NAME + " values(Ampara,hospital,Ampara General Hospital,0112456852);");
        db.execSQL("insert into " + TABLE_NAME + " values(Anuradhapura,bar,Anuradhapura Bar,011245852);");
        db.execSQL("insert into " + TABLE_NAME + " values(Colombo,retail,Colombo Supermarket,0112546852);");
        db.execSQL("insert into " + TABLE_NAME + " values(Gampaha,flight,Gampaha Airport,0112455552);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //add more insert query if you need to add more datas after, but you have first to upgrade your DATABASE_VERSION to a higher number

    }

}

this is retrieval part

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_check);

        SQLiteDatabase db = openOrCreateDatabase("dbumbers", 1, null);

        Cursor c = db.rawQuery("SELECT * FROM numbers", null);

        //c.moveToFirst();
        while (c.moveToNext()) {
            String district = c.getString(0);
            String category = c.getString(1);
            String description = c.getString(2);
            String number = c.getString(2);

            String row = district + "-" + category + "-" + description + "-" + number;

            Toast.makeText(this, row, Toast.LENGTH_LONG).show();

        }


        db.close();



    }
1
  • Try uninstalling and reinstalling. Commented Feb 26, 2015 at 16:13

4 Answers 4

1

You should be using DatabaseHandler to open your database, not using openOrCreateDatabase. Change the constructor like so (because you only need one of the arguments):

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

Then get a database like so:

DatabaseHandler dbHandler = new DatabaseHandler(this /*context*/);
SQLiteDatabase db = dbHandler.getReadableDatabase();
Cursor c = db.rawQuery(...);
Sign up to request clarification or add additional context in comments.

Comments

0

For db operations, I use a helper class like this:

public class DBAdapter {
private static final String DATABASE_NAME = "DBNAME";
private static final int DATABASE_VERSION = 1;

private static DBAdapter sInstance;

private final Context mContext; 

public DatabaseHelper DBHelper;
public SQLiteDatabase mDb;

private DBAdapter(Context ctx)     {
    this.mContext = ctx.getApplicationContext();
    DBHelper = new DatabaseHelper(mContext);
} 

public static DBAdapter getInstance(Context context) {
    if (sInstance == null) {
      sInstance = new DBAdapter(context.getApplicationContext());
    }
    return sInstance;
  }

public static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(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) {          
        ...
    }



 }    

//---opens the database---
public DBAdapter open() throws SQLException {
    mDb = DBHelper.getWritableDatabase();
    return this;
}
//add any db functions you want here


 public Cursor getCursor(int var) {
    ...do stuff here with the mDb database
}

This uses a singleton pattern so only one connection to the db is used at all times.

From an activity, I access the db like this:

DBAdapter mDb = DBAdapter.getInstance(getApplicationContext());  
mDb.open();
    Cursor cur= mDb.getCursor(2);

You dont need to close the db as only one connection will be used, it will be closed when the app closes but you could also add this after the open() function

public void close() {
    DBHelper.close();
}

Comments

0

Try changing the db name to:

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

2 Comments

the filename doesn't matter.
I have had different experiences with the filename, and found that it fix some db creation issues. That"s why I told him to try.
0

You can do something like this to work with SQLite

1)Create a databaseHandler class that will extend SQLieOpenHelper

public class EventRegister extends SQLiteOpenHelper{

private static Context context;
//Database Version
private static final int DATABASE_VERSION=6;

//Database Name
private static final String DATABASE_NAME="event_db";

//Table Names
private static final String TABLE_NAME="event_table";

//Database Constants I am taking some constants as demo you can take   your own
private static final String COLUMN_ID="id";
private static final String COLUMN_TITLE="event_title";
private static final String COLUMN_CATEGORY="event_category";
private static final String COLUMN_DATE="event_date";
private static final String COLUMN_TIME="event_time";
private static final String COLUMN_DESCRIPTION="event_description";


//Command to create a Table
private static final String CREATE_TABLE="create table "+TABLE_NAME+"("
        +COLUMN_ID+" integer primary key autoincrement,"
        +COLUMN_TITLE+" text,"
        +COLUMN_CATEGORY+" text,"
        +COLUMN_DATE+" text,"
        +COLUMN_TIME+" text,"
        +COLUMN_DESCRIPTION+" text )";



public EventRegister(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context=context;


}


/*
 *This method will be called on Start
 */
@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_TABLE);
}


/*
 * This method will be called onUpgradate of Database
 */

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}


    //Method for adding data in database EventData is my Model Object You can insert value as Single or Using Model class also
    public void addEvent(EventData taskdata)
{
            int flag=0;
    SQLiteDatabase db=this.getWritableDatabase();

    ContentValues contentValues=new ContentValues();

    contentValues.put(COLUMN_TITLE, taskdata.getEvent_title());
    contentValues.put(COLUMN_CATEGORY, taskdata.getEvent_category());
    contentValues.put(COLUMN_DATE, taskdata.getEvent_date());
    contentValues.put(COLUMN_TIME, taskdata.getEvent_time());
    contentValues.put(COLUMN_DESCRIPTION, taskdata.getEvent_description());


    flag=db.insert(TABLE_NAME, null, contentValues);
    db.close();
}


     //Add Other methods here

And from MainActivity or else you want to get data can use someThing like

EventRegister eventRegister=new EventRegister(context);
eventRegister.addData(EventData);

I hope you got some direction how to work with SQLite

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.