I have this code for my database
public class DbDatabase {
public static final String KEY_ROWID = "_id";
public static final String KEY_NOTES = "notes";
// Set up the database
public static final String DATABASE_NAME = "NotesDatabase";
public static final String DATABASE_TABLE = "notesTable";
public static final int DATABASE_VERSION = 1;
private final Context myContext;
private DbHelper myHelper;
private SQLiteDatabase myDatabase;
// Set up a subclass to help set up the database
public static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NOTES + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// If the table exists, drop it and than call onCreate method
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
onCreate(db);
}
}
public DbDatabase(Context c) {
// TODO Auto-generated constructor stub
myContext = c;
}
// Open the database
public DbDatabase open() throws SQLException {
// TODO Auto-generated method stub
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
return this;
}
// Write to the database
public long createEntry(String notes) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NOTES, notes);
return myDatabase.insert(DATABASE_TABLE, null, cv);
}
}
// Close the database
public void close() {
// TODO Auto-generated method stub
myHelper.close();
}
public ArrayList<String> getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_NOTES };
// Read info through cursor
Cursor c = myDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
ArrayList<String> result = new ArrayList<String>();
int iNotes = c.getColumnIndex(KEY_NOTES);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
// Read all the data from the beggining as far as
// the position of the first row is not after the position
// of the last row
result.add(c.getString(iNotes));
return result;
}
}
this is my adapter
public class DbDatabase {
// Set up the columns of the database
public static final String KEY_ROWID = "_id";
public static final String KEY_NOTES = "notes";
public static final String DATABASE_NAME = "NotesDatabase";
public static final String DATABASE_TABLE = "notesTable";
public static final int DATABASE_VERSION = 1;
private final Context myContext;
private DbHelper myHelper;
private SQLiteDatabase myDatabase;
// Set up a subclass to help set up the database
public static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NOTES + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// If the table exists, drop it and than call onCreate method
db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
onCreate(db);
}
}
public DbDatabase(Context c) {
// TODO Auto-generated constructor stub
myContext = c;
}
public DbDatabase open() throws SQLException {
// TODO Auto-generated method stub
myHelper = new DbHelper(myContext);
myDatabase = myHelper.getWritableDatabase();
return this;
}
public long createEntry(String notes) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NOTES, notes);
return myDatabase.insert(DATABASE_TABLE, null, cv);
}
public void close() {
// TODO Auto-generated method stub
myHelper.close();
}
public ArrayList<String> getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_NOTES };
// Read info through cursor
Cursor c = myDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
ArrayList<String> result = new ArrayList<String>();
int iNotes = c.getColumnIndex(KEY_NOTES);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
result.add(c.getString(iNotes));
return result;
}
}
I try through CAB to delete item i have chosen from my database. How can I delete them from my database?
Aucun commentaire:
Enregistrer un commentaire