jeudi 26 février 2015

SQLite database not created by SQLite Open Helper

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();



}

Aucun commentaire:

Enregistrer un commentaire