mardi 29 septembre 2015

Table is not found in SQLiteDatabase

I am facing this strange problem that I searched alot, but no use so far.

The following code I am using to retrieve SQLite database using a DBHelper that extends from openhelper class.

public class DBHelper extends SQLiteOpenHelper {

public static final int DB_VERSION = 1;
public static final String DB_NAME = "cp.db";
public static final String DB_PATH = "/data/data/http://ift.tt/1Genuo6";
public static final String ID = "no";
public static final String NAME = "name";
public static final String FORMULA = "formula";
public static final String MW = "mw";
public static final String OMEGA = "omega";
public static final String TC = "tc";
public static final String PC = "pc";
SQLiteDatabase db;
Cursor cursor;
ArrayList<String> elementName;

public DBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("drop table if exists elements");
    this.onCreate(db);

}


public List<Element> getAllElements() {
    List<Element> elementList = new ArrayList<Element>();
    // Select All Query
    Cursor cursor = null;
        String selectQuery = "SELECT  * FROM elements";
        SQLiteDatabase db = this.getReadableDatabase();
        cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Element element = new Element();
            element.setNo(Integer.parseInt(cursor.getString(0)));
            element.setFormula(cursor.getString(1));
            element.setName(cursor.getString(2));

            // Adding contact to list
            elementList.add(element);
        } while (cursor.moveToNext());
    }

    // return contact list
    return elementList;
}
}

This is capture from MainActivity class

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DBHelper helper = new DBHelper(this);

    List<Element> elementList = helper.getAllElements();

    for(Element element : elementList){
        String log = "no: "+element.getNo();

        Log.d("No: ", log);
    }

 }
}

I am getting this error:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{http://ift.tt/1GenrIX}: 
android.database.sqlite.SQLiteException: no such table: elements (code 1):  
, while compiling: SELECT  * FROM elements

I know that elements table is there

Any help?

Aucun commentaire:

Enregistrer un commentaire