0

I want to remove all the values from sqlite table. For doing this i have created one method:-

     public void deleteCourse() {

        // on below line we are creating
         SQLiteDatabase db = this.getWritableDatabase();
        // a variable to write our database.
        db.execSQL("DELETE FROM "+TABLE_NAME);
        db.close();
    }

But delete query is not working. Is there any solution to do so?

below is my complete code is database.java file

public class DBHandler extends SQLiteOpenHelper {

    // creating a constant variables for our database.
    // below variable is for our database name.
    private static final String DB_NAME = "testdb";
  public static   ArrayList<String> arrayList=new ArrayList<>();
    public static   ArrayList<Double> arraysum=new ArrayList<>();
     // below int is our database version
    private static final int DB_VERSION = 1;
    static String error;
    public static String getError(){
        return error;
    }

    // below variable is for our table name.
    private static final String TABLE_NAME = "hello";

    // below variable is for our id column.

    // below variable is for our course name column
    private static final String NAME_COL = "name";
    private static final String NAME_NUM = "count";


    private String[] hello;


    // creating a constructor for our database handler.
    public DBHandler(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    // below method is for creating a database by running a sqlite query
    @Override
    public void onCreate(SQLiteDatabase db) {
        // on below line we are creating
        // an sqlite query and we are
        // setting our column names
        // along with their data types.
        String query = "CREATE TABLE " + TABLE_NAME + " ("
                + NAME_COL + " TEXT ,"+NAME_NUM+" int )";

        // at last we are calling a exec sql
        // method to execute above sql query
        db.execSQL(query);

    }

    // this method is use to add new course to our sqlite database.
    public void addNewCourse(String courseName,int num) {

        // on below line we are creating a variable for
        // our sqlite database and calling writable method
        // as we are writing data in our database.
        SQLiteDatabase db = this.getWritableDatabase();

        // on below line we are creating a
        // variable for content values.
        ContentValues values = new ContentValues();

        // on below line we are passing all values
        // along with its key and value pair.
        values.put(NAME_COL, courseName);

        values.put(NAME_NUM,num);

        // after adding all values we are passing
        // content values to our table.
        db.insert(TABLE_NAME, null, values);

        // at last we are closing our
        // database after adding database.
        db.close();
    }

    // we have created a new method for reading all the courses.
    public ArrayList<CourseModal> readCourses() {
        // on below line we are creating a
        // database for reading our database.
        SQLiteDatabase db = this.getReadableDatabase();

        // on below line we are creating a cursor with query to read data from database.
        Cursor cursorCourses = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

        // on below line we are creating a new array list.
        ArrayList<CourseModal> courseModalArrayList = new ArrayList<>();



        // moving our cursor to first position.
        if (cursorCourses.moveToFirst()) {
            do {
                // on below line we are adding the data from cursor to our array list.
                courseModalArrayList.add(new CourseModal(cursorCourses.getString(0),cursorCourses.getString(1)));
                     arrayList.add(cursorCourses.getString(0));
                arraysum.add(cursorCourses.getDouble(1));



            } while (cursorCourses.moveToNext());
            // moving our cursor to next.
        }
        // at last closing our cursor
        // and returning our array list.
        cursorCourses.close();
        return courseModalArrayList;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // this method is called to check if the table exists already.
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

     public void deleteCourse() {

        // on below line we are creating
         SQLiteDatabase db = this.getWritableDatabase();
        // a variable to write our database.
        db.execSQL("DELETE FROM "+TABLE_NAME);
        db.close();
    }


}

Rishi Ff
  • 263
  • 2
  • 11
  • Does this answer your question? [How to delete all records from table in sqlite with Android?](https://stackoverflow.com/questions/9599741/how-to-delete-all-records-from-table-in-sqlite-with-android) – XtremeBaumer Jun 09 '22 at 07:11
  • No. Actually my app is working with all cruid operations except delete query – Rishi Ff Jun 09 '22 at 07:49

1 Answers1

0

Try this.

db.delete(DATABASE_TABLE, null, null);
Hamid-Ghasemi
  • 275
  • 2
  • 6