public class DBHandler { private Context ctx; private DBHelper helper; private SQLiteDatabase db;
public DBHandler(Context ctx){ this.ctx = ctx; helper = new DBHelper(ctx); db = helper.getWritableDatabase(); }
public static DBHandler open(Context ctx) throws SQLException{ DBHandler handler = new DBHandler(ctx); return handler; }
public void close(){ helper.close(); }
public long insert(String name,String number,String mail){ ContentValues values = new ContentValues(); values.put("name",name); values.put("number",number); values.put("mail",mail); long result = db.insert("dbtable",null, values); return result; }
public Cursor selectAll(){ Cursor cursor = db.query(true, "dbtable", new String[] {"id","name","number","mail"}, null, null, null, null, null, null); return cursor; }
public long deleteAll(String name){ long result = db.delete("dbtable", "name= '" + name + "'", null);
public class DBHelper extends SQLiteOpenHelper { private static final String DB_NAME = "dbz"; private static final int DB_VER = 1; public DBHelper(Context context){ super(context,DB_NAME,null,DB_VER); }
public void onCreate(SQLiteDatabase db){ String sql = "create table dbtable(" +"id integer primary key autoincrement," +"name text not null, number text not null, mail text not null)"; db.execSQL(sql); }
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ db.execSQL("drop table if exist dbtable"); onCreate(db); }