Method db.delete some rows? - android

In the table I have 4 fields (note1, note2, note3, Note4). I want to delete all the data in note1 and note2. I have to add arguments to the method db.delete, but I have trouble.
public void delete() {
SQLiteDatabase db= mHelper.getWritableDatabase();
db.delete(noteTable.TABLE_NAME, null, null);
}

Delete always deletes entire rows.
To clear values in specific columns, use an update query, for example
ContentValues cv = new ContentValues();
cv.putNull("note1");
cv.putNull("note2");
db.update(noteTable.TABLE_NAME, cv, null, null);

Related

delete all rows from sql in android studio

my app should delete a specific row in the sql database by this method
public void delete(int id){
open();
sqLiteDatabase.delete("item","id = "+id,null);
close();
}
and it should also delete all rows if the user want that by this method
public void deleteAll(){
cursor=sqLiteDatabase.rawQuery("select * from item",null);
cursor.moveToFirst();
i=1;
while (!cursor.isAfterLast()) {
sqLiteDatabase.delete("item","id = "+i,null);
i++;
System.out.println(i);
cursor.moveToNext();
}
}
when the user use deleteAll() alone it is work but when he use it after using delete() it delete all rows before the deleted one using delete()
is the problem in deleteAll() method ? and how to fix it?
Your delete() function only deletes rows with the given id. If you want to delete all rows, then just do
sqLiteDatabase.delete("item", null ,null);
There is no reason to write your own loop especially since you never use the Cursor anyway.
Additionally, you should never use string concatenation for the "where" clause in a SQL statement. Instead use "id = ?" and provide a String[] with the values:
db.delete("image","id= ?", new String[] {id});
Here is an example of how to delete a particular column with an id:
SQLiteDatabase db= this.getWritableDatabase();
db.delete("image","id= ?", new String[] {id});
Toast.makeText(context,"Delete successfully",Toast.LENGTH_LONG).show();
Hope this will work for you

Inserting rows into table only once in Android Database

I want to only insert like 10 rows into a table at database creation. I do not want them added every time the app is open. Just once as a table with final values. How would I go about doing this? Thanks!
public void insertEvents(){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_EVENT, "value1");
values.put(COL_EVENT, "value2");
values.put(COL_EVENT, "value3");
values.put(COL_EVENT, "value4");
db.insert(TABLE_EVENTS, null, values);
}
Since you are using SQLiteOpenHelper, put the inserts in its onCreate(). Use the SQLiteDatabase passed in as argument, do not recursively call getWritableDatabase().
Do it in onCreate method of DatabaseHelper
EDIT: here is the similar question. Or you can try this tutorial
I know this might be too late but I'm just sharing my answers. Your code seems to show that you are trying to add 4 rows. Here are some pointers:
1) Do it in the onCreate method, as mentioned by #Georgiy Shur.
2) The insert method only inserts one row at a time. So you should do this instead:
ContentValues values = new ContentValues();
values.put(COL_EVENT, "value1");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value2");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value3");
db.insert(TABLE_EVENTS, null, values);
values.put(COL_EVENT, "value4");
db.insert(TABLE_EVENTS, null, values);

How to keep the database updated when the user enters new values in android studio?

I'm dealing with a simple app, which basically stores various dates. But when I enter a new date, I have to close the app and open it again. The same thing counts for changing the date values. I can only see the changes by closing the app and reopening it.
Here are the two main functions in my DB class;
public void addBirthday(Person person){ //Add a data to database
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
db.insert(TABLE_NAME, null, cv);
db.close();
}
public int updateBirthday(Person person) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
// 3. updating row
int i = db.update(TABLE_NAME, //table
cv, // column/value
column_id +" = ?", // selections
new String[] { String.valueOf(person.getId()) }); //selection args
// 4. close
db.close();
return i;
}
You will need to refresh the view (probably a ListView) where you display this data.
Get rid of the notifyDataSetChanged() and call requery() on your Cursor

Edit all the values in a column in SQL

I can't find anything about this on the web.
I need to edit all values in a column in my database but I can't find a way.
I'm doing:
if (upgradeCursor.moveToFirst()){
do {
db.editListsColumn("0");
} while (upgradeCursor.moveToNext());
}
Where upgradeCursor:
return mDb.query(ProductsData.PRODUCTS_TABLE, null, null, null, null, null, null);
And editListsColumn("0"):
public void editListsColumn(String lists) {
mDb=mDbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ProductsMetaData.PRODUCT_PRODUCT, lists);
mDb.update(ProductsData.PRODUCTS_TABLE, cv, "lists=?", null);
}
I'm stuck here.
Any suggestion would be really appreciated at the moment! Thank you so much.
Have you tried a simple update without the where clause and without iterating the cursor. What I mean is simply call this:
mDb=mDbHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(ProductsMetaData.PRODUCT_PRODUCT, lists);
mDb.update(ProductsData.PRODUCTS_TABLE, cv, null, null);
Without:
if (upgradeCursor.moveToFirst()){
do {
db.editListsColumn("0");
} while (upgradeCursor.moveToNext());
}
From the documentation of the update(String table, ContentValues values, String whereClause, String[] whereArgs) method: passing NULL in whereClause will update all rows.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

Names are getting overlapped in SQLite

In my Android App, I am trying to insert names in a column forcefully into database but In database only last name is showing i.e. Myself because names are overlapping. Actually My database portion is weak.
This is what I am doing in my Database Handler Class,
public void addPoliticianNames()
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
values.put(KEY_POLITICIANNAMES, "Me");
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
}
What to do for getting these names sequentially into the column (Here I have to insert names forcefully as I have mentioned above). So, Is there any method for this ? Is there any role of cursor in doing this job (like movetonext method or something like that) ?
Please help me,
Thanks.
you have to make one loop for inserting data into database. it will pick one by one value from array and put it into database.
Put this method in your database class
public void deleteTable()
{
database.delete(TABLE_POLLINGVOTES, null, null);
}
db.deleteTable(); // call it with this line for delete all records of your table
// for inserting data into table.
String[] a = {"I","Me","MySelf"};
ContentValues values = new ContentValues();
for (int i = 0; i < a.length; i++)
{
values.put(KEY_POLITICIANNAMES, a[i]);
db.insert(TABLE_POLLINGVOTES, null, values);
}
Hope it will help you.
If you want to insert three records, you have to use three insert calls.
For example:
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, "I");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Me");
db.insert(TABLE_POLLINGVOTES, null, values);
values.put(KEY_POLITICIANNAMES, "Myself");
db.insert(TABLE_POLLINGVOTES, null, values);
#Shiv change the column name for every value
public void addPoliticianNames(String ss)
{
ContentValues values=new ContentValues();
values.put(KEY_POLITICIANNAMES, ss);
}
ss contain value firts time "I",second time "me" like that

Categories

Resources