Pass data to delete method - Android Database

Android examples for Database:Table Row Delete

Description

Pass data to delete method

Demo Code


import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class Main{
    public static SQLiteDatabase db;
    public static String dbname = "/storage/sdcard0/Handler.db";
    public static String tablename = "cowlist";
    public static String detailname = "detaillist";
    public static String workname = "worklist";
    public static boolean deleteData(listdata deletedata) {
        String delnum = deletedata.number;
        openDatabase(tablename);/*  w  w  w  .  j ava2s .com*/
        openDatabase(detailname);
        openDatabase(workname);

        db.delete(tablename, "number=?", new String[] { delnum });
        db.delete(detailname, "number=?", new String[] { delnum });
        db.delete(workname, "cownumber=?", new String[] { delnum });
        closeDatabase();

        return true;
    }
    public static void openDatabase(String dbName) {
        try {
            //db= SQLiteDatabase.openOrCreateDatabase(dbname,Activity.MODE_MULTI_PROCESS,null);
            db = SQLiteDatabase.openDatabase(dbname, null,
                    SQLiteDatabase.OPEN_READWRITE
                            + +SQLiteDatabase.CREATE_IF_NECESSARY);
            Log.d("DataBase open ", "open");
        } catch (SQLiteException ex) {
            //Toast.makeText(this, ex.getMessage(), 1).show();
            Log.d("DataBase open fail", "fail");
        }
    }
    public static void closeDatabase() {
        try {
            Log.d("DB close", "Close");
            // close database
            db.close();
        } catch (Exception ext) {
            ext.printStackTrace();
            Log.d("closeDatabase",
                    "Exception in closing database : " + ext.toString());
        }
    }
}

Related Tutorials