I have used a separate class for creation of db. In that I have written the delete function like this
public void name_delete(String name){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_NAME + "=" + name, null);
//KEY_NAME is a column name
}
In main class I called this function
db.name_delete(""+all_names.getSelectedItem().toString());
all_names.getSelectedItem().toString() is a spinner selected item. To delete the particular row with the name selected in spinner. Help me how to write the function.
Should be
db.delete(TABLE_NAME, KEY_NAME + "=?", new String[]{name});
Also
db.name_delete(all_names.getSelectedItem().toString());
public void name_delete(String name){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME, KEY_NAME +"=?", new String[]{name});
}
USE this
Related
As my title question, I want to delete some rows of table on SQLite where contains specific string.
Here are my methods I tried but there are no any row is deleted. I checked table of SQLite database by get it out and put in to DB Browser for SQLite which is downloaded from https://sqlitebrowser.org/
public void delete1(String table,String COLUMN,String link) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM "+table+" WHERE "+COLUMN+" LIKE "+link+"%");
}
public void delete2(String table,String name){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(table, "PRODUCTNAME" + "LIKE ?", new String[]{name+"%"}) ;
}
Could you tell me how to do it or how have i to correct code ?
using db.delete(table, "PRODUCTNAME " + "LIKE ?", new String[]{name+"%"}) ; will only delete rows that start with the value in name.
Perhpas you want :-
db.delete(table, "PRODUCTNAME " + "LIKE ?", new String[]{"%"+name+"%"}) ;
Then it would delete rows that contain the value rather than start with the value.
With db.execSQL("DELETE FROM "+table+" WHERE "+COLUMN+" LIKE "+link+"%"); you need to enclose the string in single quotes and assuming that you want to delete a row that contains the value then use :-
db.execSQL("DELETE FROM "+table+" WHERE "+COLUMN+" LIKE '%"+link+"%'");
Using the delete convenience method (the first part) is the better option as it protects against SQL Injection, it properly encloses the value, builds the underlying SQL and also returns the number of affected (deleted) rows.
If you use the following, this will write dome debugging information that may assist in debugging :-
public void delete2(String table,String name){
SQLiteDatabase db = this.getWritableDatabase();
Log.d("DELETEINFO","Attempting to delete rows with \n\t->" + name);
int deletedCount = db.delete(table, "PRODUCTNAME " + "LIKE ?", new String[]{"%"+name+"%"}) >0) ;
Log.d("DELETEINFO","Deleted " + deletedCount + " rows.");
}
public void(Budget budget){
SQLiteDatabase db = this.getReadableDatabase();
db.delete(Budget.table_name, Budget.collum_id, " = ?", new String[]{String.valueOf(budget.getId())}
}
You provided wrong parameters to delete method, right syntax is as below
delete(String table, String whereClause, String[] whereArgs)
if you are confused with this syntax simply use
db.execSQL(DELETE FROM table WHERE condition1 AND condition2.....);
change your code this way you are getting data for reading.
public void(Budget budget){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Budget.table_name, Budget.collum_id, " = ?", new String[]{String.valueOf(budget.getId())
db.close();}
}
Change to:
SQLiteDatabase db = this.getWriteableDatabase();
db.delete(Budget.table_name, Budget.collum_id + " = ?", new String[]{String.valueOf(budget.getId())});
You want to delete not read from the db
Also change to
Budget.collum_id + " = ?"
I am playing with an example of SQLite I found on the internet. I have an update statement like this:
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?" +
contact.getID(), null);
}
And an update statement like this:
public int updateContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[]{String.valueOf(contact.getID())});
}
Can someone tell me the difference?
Here is the method signature for an update operation on SQLite database.
int android.database.sqlite.SQLiteDatabase.update(
String table, ContentValues values, String whereClause, String[] whereArgs)
From developer.android.com
You may include ?s in the where clause, which will be replaced
by the values from whereArgs. The values will be bound as Strings.
Btw your first example wouldn't work cause you have included
"KEY_ID + " = ?" + contact.getID()" in whereClause param and kept the whereArgs null. The ?s would'nt be replaced by your arg contact.getId()
Change whereClause in 1st example to this: KEY_ID + " = " + contact.getID()
On your 1st example.The code can't update anything.
The update method whereCause params will be convert to some SQL on where case ,It will replace the ? placeholder with whereArgs.
Such as:
In your 1st example.If contact.getId() return 1,The final SQL is like:
update contact set KEY_NAME = 'your contact name ' where KEY_ID = ? 1
but 2st example final SQL is like:
update contact set KEY_NAME = 'your contact name ' where KEY_ID = 1
So,the first example is not work.
I have a database called "Users" and a table in that database called "contact". i want to delete a selected contact when I click a button. I want to know how to set the parameters correctly to the delete method. if somebody can gimme an answer with a little example i will be delighted .
my code goes like this
private void deleteContact(String name) {
SQLiteDatabase database=openOrCreateDatabase("Users",MODE_PRIVATE,null);
int res=database.delete("contact", "name =", name);
///
}
I call this method when I click that Button. my query is,
"delete from contact where name ='"+name+"';
If your datebase table name is Contact and name is unique identifier and name of column you are inserting names is COLUMN_NAME then
//Delete single item from Db
public void deleteContact(String name){
SQLiteDatabase db = this.getWritableDatabase();
db.delete("Contact", COLUMN_NAME + " = ?", new String[]{name});
db.close();
}
private void deleteContact(String name) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete("contact","name=?" , new String[]{name});
db.close();
}
I have a table called app_catagory. The problem is, when I try to insert a row into the table, I got this exception:
FATAL EXCEPTION : main java.lang.NullPointerException
The LogCat shows this line as the offending code:
db.insert(DATABASE_TABLE2, null, initialValues);
How can I fix it?
Code
static final String DATABASE_APP_CAT= "create table app_catagory(cat_id interprimary key autoincrement, " +
"name text not null," +
"date_created date , " +
"img_name text);";
//Insert into catagory table
public static void insertContact_app_cat()
{
String[] name={"DOCTOR","PHARMACY","PATHLAB","BLOODBANK"};
for(int i=1;i<=4;i++){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CAT_NAME, name[i]);
initialValues1.put(KEY_CAT_DATE, "05-05-2014");
initialValues2.put(KEY_CAT_IMG,"null");
db.insert(DATABASE_TABLE2, null, initialValues);
}
}
I don't see where you are getting the db when I do an insert I call this piece of code before doing my ContentValues add stuff.
SQLiteDatabase db = this.getWritableDatabase();
Then after the insert I would close the database.
db.close();