jeudi 5 mai 2016

table sites has no column named image (code 1): , while compiling: INSERT INTO sites(discription,image,name) VALUES (?,?,?)

I am creating a database and trying to insert and retrieve values. But when I try to insert values the following error is shown on Logcat

table sites has no column named image (code 1): , while compiling: INSERT INTO sites(discription,image,name) VALUES (?,?,?)

This is the code for SQLiteOpenHelper class



    package com.sahar.wave.DB;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.sahar.wave.DivingSite;
import com.sahar.wave.User;

import java.io.ByteArrayOutputStream;

public class SiteDBMngr {

    public static final String DB_NAME = "WaveDB";
    public static final String TASKS_TABLE = "sites";
    public static final int DB_VAR = 2;

    public static final String TASK_NAME = "name";
    public static final String TASK_IMAGE = "image";
    public static final String TASK_DISCRIPTION = "discription";




    private static final String SCRIPT_CREATE_DB = "create table " + TASKS_TABLE + "(" +
            TASK_NAME + " text primary key , image BLOB, "+
            TASK_DISCRIPTION + " text not null" + ");";

    private Context context;
    private MyDBHelper myDBHelper;

    public SiteDBMngr(Context context) {
        this.context = context;
        this.myDBHelper = new MyDBHelper(this.context);
    }

    public void addSite(DivingSite site)
    {

        SQLiteDatabase database = myDBHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(TASK_NAME, site.getName());
        if(site.getImage() != null) {
            byte[] bt = DbBitmapUtility.getBytes(site.getImage());
            values.put(TASK_IMAGE,   bt);
        }
        else {
            byte[] bt = null;
            values.put(TASK_IMAGE,   bt);
        }
        values.put(TASK_DISCRIPTION, site.getDiscription());
        database.insert(TASKS_TABLE, null, values);
        database.close();
    }


    public boolean deleteSite(String name) {
        SQLiteDatabase database = myDBHelper.getWritableDatabase();
        boolean b = database.delete(TASKS_TABLE, TASK_NAME + "=" + name, null) > 0;
        database.close();
        return b;
    }

    public Cursor getCursorAll() {
        Cursor cursor;
        SQLiteDatabase database = myDBHelper.getReadableDatabase();
        cursor = database.query(TASKS_TABLE, new String[]{TASK_NAME,TASK_IMAGE, TASK_DISCRIPTION}, null, null, null, null, null, null);
        return cursor;
    }

    public String[] getList(String st) {
        SQLiteDatabase database = myDBHelper.getReadableDatabase();
        //Cursor crs = database.rawQuery("Select * from" + TASKS_TABLE + "where name like '%" + st + "%'", null);
        Cursor crs = database.query(TASKS_TABLE, new String[]{TASK_NAME}, "name like '%" + st + "%'", null, null, null, null, null);
        String query = "Select * from" + TASKS_TABLE + "where name like '%" + st + "%'";
        database.execSQL(query);
        Cursor cursor = database.query(TASKS_TABLE, new String[]{TASK_NAME,TASK_IMAGE, TASK_DISCRIPTION}, null, null, null, null, null, null);
        String[] array = new String[cursor.getCount()];
        int i = 0;
        while(cursor.moveToNext()){
            String uname = cursor.getString(cursor.getColumnIndex("name"));
            array[i] = uname;
            i++;
        }

        return array;
    }

    public void printAllCursorDB() {
        Cursor cursor = getCursorAll();
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            StringBuffer st = new StringBuffer();
            st.append("name: " + cursor.getString(0));
            st.append(" ,  discription: " + cursor.getString(1));
            Log.i("MyTasksDBMngr", st.toString());
            cursor.moveToNext();
        }
        cursor.close();
    }

    private class MyDBHelper extends SQLiteOpenHelper {

        public MyDBHelper(Context context) {
            super(context, DB_NAME, null, DB_VAR);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            String sqlst = String.format("drop table if exists %s", TASKS_TABLE);
            db.execSQL(sqlst);
            db.execSQL(SCRIPT_CREATE_DB);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TASKS_TABLE);
        db.execSQL(SCRIPT_CREATE_DB);
        // create new table
        onCreate(db);}

    }

Can you please say where I have gone wrong? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire