0

I have a problem accessing the DB of my app. I have to load some data from the DB but I keep getting this error:

    09-11 11:29:36.032 21955-23129/com.stella.erica.cookingtime E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
        Process: com.stella.erica.cookingtime, PID: 21955
        java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:300)
          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
          at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
          at java.util.concurrent.FutureTask.run(FutureTask.java:242)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
          at java.lang.Thread.run(Thread.java:841)
        Caused by: android.database.sqlite.SQLiteException: no such table: type (code 1): , while compiling: SELECT type, image FROM type
          at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
          at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
          at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
          at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
          at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
          at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
          at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
          at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
          at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
          at com.stella.erica.cookingtime.TypesDataSource.getListofTypes(TypesDataSource.java:36)
          at com.stella.erica.cookingtime.ChoiceActivity$AddChoicesTask.getListOfChoices(ChoiceActivity.java:179)
          at com.stella.erica.cookingtime.ChoiceActivity$AddChoicesTask.doInBackground(ChoiceActivity.java:108)
          at com.stella.erica.cookingtime.ChoiceActivity$AddChoicesTask.doInBackground(ChoiceActivity.java:103)
          at android.os.AsyncTask$2.call(AsyncTask.java:288)
          at java.util.concurrent.FutureTask.run(FutureTask.java:237)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
          at java.lang.Thread.run(Thread.java:841) 

Here's my DBhelper

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

 public class TypesDBHelper extends SQLiteOpenHelper {

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table "
            + DBContracts.TypeTable.TABLE_NAME + "( "
            + DBContracts.TypeTable.COLUMN_ID + " integer primary key autoincrement, "
            + DBContracts.TypeTable.COLUMN_TYPE + " text not null,"
            + DBContracts.TypeTable.COLUMN_IMAGE + "text not null);";

    private static final String DATABASE_INSERT_VALUES = "insert into "
            + DBContracts.TypeTable.TABLE_NAME + "("
            + DBContracts.TypeTable.COLUMN_TYPE + ","
            + DBContracts.TypeTable.COLUMN_IMAGE +
            ") values ('Main course','main_course_image'), " +
            "('Side dish','side_dish_image'), " +
            "('Dessert','dessert_image'), " +
            "('Appetizer','appetizer_image'), " +
            "('Salad','salad_image'), " +
            "('Bread','bread_image'), " +
            "('Breakfast','breakfast_image')," +
            "('Soup','soup_image')," +
            "('Beverage','beverage_image')," +
            "('Sauce','sauce_image')," +
            "('Drink','drink_image')";

    public TypesDBHelper(Context context) {
        super(context, DBContracts.DATABASE_NAME, null, DBContracts.TypeTable.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
        database.execSQL(DATABASE_INSERT_VALUES);
    }

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

}

Here is my data source access

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

public class TypesDataSource {

    private SQLiteDatabase database;
    private TypesDBHelper dbHelper;

    public TypesDataSource(Context context){
        this.dbHelper = new TypesDBHelper(context);
    }

    public void open() throws SQLException {
        database = dbHelper.getReadableDatabase();
    }

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

    public ArrayList<String[]> getListofTypes(){
        ArrayList<String[]> types = new ArrayList<>();

        String[] projection = {DBContracts.TypeTable.COLUMN_TYPE, DBContracts.TypeTable.COLUMN_IMAGE};
        Cursor cursor = database.query(DBContracts.TypeTable.TABLE_NAME, projection, null, null, null, null, null);
        cursor.moveToFirst();

        while(!cursor.isAfterLast()){
            Type type = cursorToType(cursor);
            String[] typeToString = {type.getName(), type.getImageName()};
            types.add(typeToString);
            cursor.moveToNext();
        }

        cursor.close();
        return types;
    }

    private Type cursorToType(Cursor cursor){
        Type type = new Type(cursor.getString(0), cursor.getString(1));
        return type;
    }
}

And here's the method from which I'm using my data source (it's in a subclass I made of AsyncTask):

private ArrayList<String[]> getListOfChoices(Context context){

    TypesDataSource dataSource = new TypesDataSource(context);
    try {
        dataSource.open();
    }catch (SQLException e){
        //todo
        Log.e("DBEX", "ECCEZIONE NEL DB");
    }

    ArrayList<String[]> typeList = dataSource.getListofTypes();
    dataSource.close();

    return typeList;            
}

I can't figure out why I keep getting this error especially because I have another table in the same DB (with almost identical code) and I don't have problems accessing it. Thanks in advance for your help.

Edit: I changed the column names as suggested but I get the same error with the new column names. I tried to look for an answer in the links I've been posted but I can't find it in there either...

Busghella
  • 9
  • 4

1 Answers1

2

Your error log seems to be complaining about the following SELECT query:

SELECT type, image FROM type

type is not a SQLite keyword, but one possible explanation is that SQLite cannot resolve the type object correctly because it is appearing as both a column and a table. One fix might be to use the fully qualified name of the table, including the database in which it resides, e.g.

SELECT type, image FROM yourdb.type
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I tried to change all the names but I keep getting the same error... and I couldn't find a solution even after looking at the link above... – Busghella Sep 11 '16 at 16:52