Ich habe eine App, die sqlite Datenbank und Kontaktliste von Mainactivity enthalten Ich habe Kontakte in Arraylist hinzugefügt und speichern Sie es in SQLite-Datenbank und holen Sie das gleiche aus SQLite-Datenbank. Aber porblem ist, dass ich bestimmte Reihe von sqlite Datenbank mit id löschen möchte. Wie mache ich dasWie lösche Zeile aus SQLite-Datenbank mit ID in Android?
Code von Mainactivity: -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Ravi", "9100000000"));
db.addContact(new Contact("Srinivas", "9199999999"));
db.addContact(new Contact("Tommy", "9522222222"));
db.addContact(new Contact("Karthik", "9533333333"));
// Reading all contacts
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: " + cn.getID() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
spezifische funtion SQLite databas Kontakte zu löschen.
// Deleting single contact
public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
Sie Abfrage ist korrekt, um eine einzelne Daten zu löschen, überprüfen Sie Ihr Protokoll haben Sie einen Fehler im Protokoll. –
Ich weiß nicht, wie man Artikel mit der ID – Niraj
löschen Was Sie getan haben, ist richtig, wie Sie sagen, es ist nicht gelöscht ..? und überprüfen Sie Ihr Protokoll, haben Sie einen Absturz. –