-3

I am trying to get data from my database but the app collapses when i do so.

Please check out my code and help me, I'v tried to fix it but I couldn't find what's the problem.

That's what I am trying to save:

public class SinglePet {

private String name;
private Bitmap photo;
private String task1;
private String task2;
private String task3;

private long id;

public SinglePet(String name, Bitmap photo, String task1, String task2, String task3, long id) {
    this.name = name;
    this.photo = photo;
    this.task1 = task1;
    this.task2 = task2;
    this.task3 = task3;
    this.id = id;
}

 //and getter and setter...

This is my database:

public class MyPetsOpenHelper extends SQLiteOpenHelper {

public static final String DATABASENAME="pets.db";
public static final String TABLE_MYPETS="tblmypets";
public static final int DATABASEVERSION=1;

public static final String COLUMN_ID="id";
public static final String COLUMN_IMAGE="image";
public static final String COLUMN_NAME="name";
public static final String COLUMN_TASK1="task1";
public static final String COLUMN_TASK2="task2";
public static final String COLUMN_TASK3="task3";

private static final String CREATE_TABLE_MYPETS="CREATE TABLE IF NOT EXISTS " +
        TABLE_MYPETS + "(" + COLUMN_ID +  " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " VARCHAR," + COLUMN_IMAGE + " BLOB,"
        + COLUMN_TASK1 + " VARCHAR," + COLUMN_TASK2 + " VARCHAR," + COLUMN_TASK3 + " VARCHAR"  +  ");";

String [] allColumns={MyPetsOpenHelper.COLUMN_ID, MyPetsOpenHelper.COLUMN_NAME, MyPetsOpenHelper.COLUMN_IMAGE,
        MyPetsOpenHelper.COLUMN_TASK1, MyPetsOpenHelper.COLUMN_TASK2, MyPetsOpenHelper.COLUMN_TASK3};

SQLiteDatabase database;

public MyPetsOpenHelper(Context context)
{
    super(context, DATABASENAME, null, DATABASEVERSION);
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_MYPETS);
    Log.d("data", "Table MyPets is created");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_MYPETS);
    onCreate(db);
}

public void open()
{
    database=this.getWritableDatabase();
    Log.d("data", "database connection open");
}

//QUERIES
public SinglePet createSinglePet(SinglePet s)
{
    ContentValues values= new ContentValues();
    values.put(COLUMN_NAME, s.getName());
    Bitmap b=s.getPhoto();
    values.put(COLUMN_IMAGE,PhotoHelper.bitmapToString(b));
    values.put(COLUMN_TASK1, s.getTask1());
    values.put(COLUMN_TASK2, s.getTask2());
    values.put(COLUMN_TASK3, s.getTask3());

    long insertId=database.insert(TABLE_MYPETS, null, values);
    Log.d("data", "MyPets " + insertId + " insert to database");
    s.setId(insertId);
    return s;
}

when I try to run this function (getAll()) the app collapses!!!

public ArrayList<SinglePet> getAll()
{
    ArrayList<SinglePet> l= new ArrayList<SinglePet>();
    Cursor cursor=database.query(MyPetsOpenHelper.TABLE_MYPETS, allColumns, null, null, null, null, null);
    if(cursor.getCount()>0)
    {
        while (cursor.moveToNext())
        {
            long id= cursor.getLong(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_ID));
            String name= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_NAME));
            byte [] image= cursor.getBlob(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_IMAGE));
            Bitmap imageB= PhotoHelper.byteArrayTBitmap(image);
            String task1= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK1));
            String task2= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK2));
            String task3= cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK3));

            SinglePet s=new SinglePet(name, imageB , task1, task2, task3, id);
            l.add(s);
        }
    }
    return  l;


// and the rest of the class doesn't matter...

the problem is when I write something like-

list=myPetsOpenHelper.getAll();

the app collapses!!!

PLEASE HELP ME

full trace:

04-14 15:46:49.913 11231-11231/com.example.admin.petcare I/data: list size is 0 04-14 15:46:49.933 11231-11231/com.example.admin.petcare D/data: database connection open 04-14 15:46:49.983 11231-11231/com.example.admin.petcare E/SQLiteLog: (1) table tblmypets has no column named task2 04-14 15:46:49.983 11231-11231/com.example.admin.petcare E/SQLiteDatabase: Error inserting image=[B@a5a2aa38 task1=task 1 task2=task 2 task3=task 3 name=example android.database.sqlite.SQLiteException: table tblmypets has no column named task2 (code 1): , while compiling: INSERT INTO tblmypets(image,task1,task2,task3,name) VALUES (?,?,?,?,?) 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.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339) at com.example.admin.petcare.MyPetsOpenHelper.createSinglePet(MyPetsOpenHelper.java:74) at com.example.admin.petcare.MyPetsActivity.examplePet(MyPetsActivity.java:182) at com.example.admin.petcare.MyPetsActivity.onCreate(MyPetsActivity.java:68) at android.app.Activity.performCreate(Activity.java:5133) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) 04-14 15:46:49.983 11231-11231/com.example.admin.petcare D/data: MyPets -1 insert to database 04-14 15:46:49.993 11231-11231/com.example.admin.petcare D/data: database connection open 04-14 15:46:49.993 11231-11231/com.example.admin.petcare E/SQLiteLog: (1) no such column: task2 04-14 15:46:49.993 11231-11231/com.example.admin.petcare D/AndroidRuntime: Shutting down VM 04-14 15:46:49.993 11231-11231/com.example.admin.petcare W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa4fdc678) 04-14 15:46:50.003 11231-11231/com.example.admin.petcare E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.petcare/com.example.admin.petcare.MyPetsActivity}: android.database.sqlite.SQLiteException: no such column: task2 (code 1): , while compiling: SELECT id, name, image, task1, task2, task3 FROM tblmypets at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5103) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.sqlite.SQLiteException: no such column: task2 (code 1): , while compiling: SELECT id, name, image, task1, task2, task3 FROM tblmypets 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.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteQuery.(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.example.admin.petcare.MyPetsOpenHelper.getAll(MyPetsOpenHelper.java:83) at com.example.admin.petcare.MyPetsActivity.onCreate(MyPetsActivity.java:72) at android.app.Activity.performCreate(Activity.java:5133) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)  at android.app.ActivityThread.access$600(ActivityThread.java:141)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:137)  at android.app.ActivityThread.main(ActivityThread.java:5103)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:525)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  at dalvik.system.NativeStart.main(Native Method)  04-14 15:46:50.013 1460-1460/system_process W/ActivityManager: Force finishing activity com.example.admin.petcare/.MyPetsActivity 04-14 15:46:50.343 1974-11625/com.google.android.gms D/DropBoxEntryAddedChimeraService: User is not opted-in to Usage & Diagnostics or Lockbox. 04-14 15:46:50.523 1460-1477/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{a5642da0 u0 com.example.admin.petcare/.MyPetsActivity} 04-14 15:46:59.723 1460-1477/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock! 04-14 15:46:59.743 1460-1477/system_process W/ActivityManager: Activity idle timeout for ActivityRecord{a5642da0 u0 com.example.admin.petcare/.MyPetsActivity} 04-14 15:47:00.533 1460-1477/system_process W/ActivityManager: Activity idle timeout for ActivityRecord{a54c27e0 u0 com.example.admin.petcare/.MainActivity} 04-14 15:47:04.733 1460-1477/system_process W/ProcessStats: Skipping unknown process pid 11730 04-14 15:47:09.753 1460-1477/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{a5642da0 u0 com.example.admin.petcare/.MyPetsActivity} 04-14 15:48:19.403 1460-1477/system_process W/BroadcastQueue: Timeout of broadcast BroadcastRecord{a572c658 u0 com.google.firebase.INSTANCE_ID_EVENT} - receiver=android.os.BinderProxy@a5d035d0, started 60021ms ago 04-14 15:48:19.403 1460-1477/system_process W/BroadcastQueue: Receiver during timeout: ResolveInfo{a572cb00 com.google.firebase.iid.FirebaseInstanceIdInternalReceiver p=0 o=0 m=0x0} 04-14 15:48:19.433 1460-1477/system_process I/ActivityManager: Crashing app skipping ANR: ProcessRecord{a5637df0 11231:com.example.admin.petcare/u0a10050} Broadcast of Intent { act=com.google.firebase.INSTANCE_ID_EVENT flg=0x14 cmp=com.example.admin.petcare/com.google.firebase.iid.FirebaseInstanceIdInternalReceiver (has extras) }

Meow
  • 11
  • 6

1 Answers1

0

I was unable to find the cause of the error which is shown by your log (You're code seemed fine). Though, I've tested your code and done some modification (Not related to the error). It is working alright. You can check the differences and modify your own code accordingly or just copy the below code. Here is the MyPetsOpenHelper.java codes of mine. (The portion you provided).

public class MyPetsOpenHelper extends SQLiteOpenHelper {

    public static final String DATABASENAME = "pets.db";
    public static final String TABLE_MYPETS = "tblmypets";
    public static final int DATABASEVERSION = 2;

    public static final String COLUMN_ID = "id";
    public static final String COLUMN_IMAGE = "image";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_TASK1 = "task1";
    public static final String COLUMN_TASK2 = "task2";
    public static final String COLUMN_TASK3 = "task3";

    private static final String CREATE_TABLE_MYPETS = "CREATE TABLE IF NOT EXISTS " +
            TABLE_MYPETS + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_NAME + " VARCHAR," + COLUMN_IMAGE + " BLOB,"
            + COLUMN_TASK1 + " VARCHAR," + COLUMN_TASK2 + " VARCHAR," + COLUMN_TASK3 + " VARCHAR" + ");";

    String[] allColumns = {MyPetsOpenHelper.COLUMN_ID, MyPetsOpenHelper.COLUMN_NAME,  MyPetsOpenHelper.COLUMN_IMAGE,
            MyPetsOpenHelper.COLUMN_TASK1, MyPetsOpenHelper.COLUMN_TASK2, MyPetsOpenHelper.COLUMN_TASK3};

    SQLiteDatabase database;

    public MyPetsOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASENAME, factory, DATABASEVERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_MYPETS);
        Log.d("data", "Table MyPets is created");
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_MYPETS);
        onCreate(db);
    }

    public void open() {
        database = this.getWritableDatabase();
        Log.d("data", "database connection open");
    }

    public void close() {
        database.close();
        Log.d("data", "database connection closed");
    }

    //QUERIES
    public SinglePet createSinglePet(SinglePet s) {
        ContentValues values = new ContentValues();
        open();
        values.put(COLUMN_NAME, s.getName());
          Bitmap b = s.getPhoto();
          values.put(COLUMN_IMAGE, PhotoHelper.bitmapToString(b));
        values.put(COLUMN_TASK1, s.getTask1());
        values.put(COLUMN_TASK2, s.getTask2());
        values.put(COLUMN_TASK3, s.getTask3());

        long insertId = database.insert(TABLE_MYPETS, null, values);
        Log.d("data", "MyPets " + insertId + " insert to database");
        s.setId(insertId);
        close();
        return s;
    }

    public ArrayList<SinglePet> getAll() {
        ArrayList<SinglePet> l = new ArrayList<SinglePet>();
        open();
        Cursor cursor = database.query(MyPetsOpenHelper.TABLE_MYPETS, allColumns, null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                long id = cursor.getLong(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_ID));
                String name = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_NAME));
                byte [] image= cursor.getBlob(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_IMAGE));
                Bitmap imageB= PhotoHelper.byteArrayTBitmap(image);
                String task1 = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK1));
                String task2 = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK2));
                String task3 = cursor.getString(cursor.getColumnIndex(MyPetsOpenHelper.COLUMN_TASK3));

                SinglePet s = new SinglePet(name, task1, task2, task3, id);
                l.add(s);
                cursor.moveToNext();
            }
            cursor.close();
            close();
        }
        return l;

    }
}

NOTE: I've changed DATABASEVERSION to 2. It will recreate the database, thus your previous tables and data will be deleted. So you have to re-input your data. Hope this helps.

tahsinRupam
  • 6,325
  • 1
  • 18
  • 34