0

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());}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Opl Itay
  • 39
  • 1
  • 5

2 Answers2

2

FYI

  • App uninstall is not good approach . If your APP is on PLAYSTORE then ?

You should Use onUpgrade for avoid DATA LOSS .

Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.

How ?

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);


        if (oldVersion < 3){ // Here I SET 3. You should use your DB version . When-ever **`adding table`**  please increase DB version .
            db.execSQL(ALTER_USER_TABLE_1);
            db.execSQL(ALTER_USER_TABLE_2);
        }

    }

ALTER TABLE

private static final String ALTER_USER_TABLE_1 =  "ALTER TABLE test_table ADD FIELD_NAME TEXT";
private static final String ALTER_USER_TABLE_2 = "ALTER TABLE test_table ADD FIELD_NAME2 TEXT";
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

There is an older version of the database on your device, which does not have the test_table table.

Solution: Uninstall your app and install again

John Joe
  • 12,412
  • 16
  • 70
  • 135