I'm using SQLiteDatabase
and i've got a weird problem - when im trying to SELECT * FROM
table its gives me this:
no such table: test_table (code 1): , while compiling: SELECT * FROM test_table
but when i insert to the same table, no Exception
is sent.
This is my code:
The method that gets all the data from the table:
public List<Test> GetAllTests()
{
List<Test> tests =new ArrayList<Test>();
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM " + TEST_TABLE;
Cursor cursor = db.rawQuery(sql,null);
if(cursor.moveToFirst())
{
do
{
Test test = new Test();
test.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
test.setSubjectID(cursor.getInt(cursor.getColumnIndex(KEY_SUBJECT_ID)));
String dateString = cursor.getString(cursor.getColumnIndex(KEY_TEST_DATE));
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = null;
try {
date = format.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
test.setDate(date);
test.setGrade(cursor.getDouble(cursor.getColumnIndex(KEY_TEST_GRADE)));
tests.add(test);
}while(cursor.moveToNext());
}
return tests;
}
The method that insert data to the same table(no Exception):
public long InsertTest(Test test)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_SUBJECT_ID,test.getSubjectID());
values.put(KEY_TEST_DATE,test.getDate().toString());
return db.insert(TEST_TABLE,null,values);
}
The code that inserts and get the data:
Test test = new Test(testSubjectID.getSelectedItemPosition(),date);
AddTestToList(test);
try {
dbManager.InsertTest(test);
List<Test> tests = dbManager.GetAllTests();
for (Test test:tests)
{
AddTestToList(test);
}
}catch (Exception e){Log.d("MSG_ERROR",e.toString());}