I want to delete all rows in a table with a specific ID (not primary key).
I have tested two different methods, but they only remove the first row it finds with the specific ID:
db.delete(CalendarTable.TABLE_NAME, "repeat_group="+repeatGroup, null);
and
db.delete(CalendarTable.TABLE_NAME, "repeat_group=?", new String[]{Integer.toString(repeatGroup)});
None of these methods works, how can I remove ALL rows in a table with this specific ID?
Thanks in advance!
UPDATE:
Lol, the method above did work! It was just me the stupid one that called the my own method delete() instead of deleteRepeatGroup(), guess I'm too tired!
Anyways, thank you guys for taking your time.
If everything else fails, you can try the following. Get all rows in the table with the ID you are trying to delete and save the rowID's in an array. Then iterate over the array and delete each row.
I hope this works as expected
you can use
String urQuery = "delete from tablename where Id in ("
+ Id + ")";
here Id may have all ids separated by comma ex. "id1,id2".
Related
This is the query that I use to create a table
create table site_table(
_id integer primary key autoincrement,
name_site text,
url text,
login text,
pass text
);
I called Cursor.getColumnNames() and noticed that columns order are id, login, pass, name, url.
So, if I want a value I have to get it by the index Cursor.getString(index). Until I debugged I was messing up calling the wrong index, but now I wonder, why SQLite saves that way? Why it does not follow that way I created id, name_site, url, login and pass?
Thanks
So, if I want a value I have to get it by the index
Cursor.getString(index)
So for example for this reason you should always use
c.getString(c.getColumnIndex("ColName")); // or better getColumnIndex(CONSTANT)
This method saves all of us and ensure that you never get wrong results. Generally this method is recommended and also storing COLUMN_NAMES as CONSTANTS in separated class is very, very useful and efficient practise.
Note: Order depends on projection i.e. select name, lastname from table
That data is ordered by the order your requested it in your query, not the order you created the table with. So you probably changed the order in your query that generated said cursor.
Columns order in your cursor depends on projection. To be sure you use correct column index use c.getString(c.getColumnIndexOrThrow("COLUMN_NAME")) where c is your cursor.
I just made the experience first hand:
The indices of the columns of the cursor as a result of a
SELECT * FROM mytable WHERE ...
query have sometimes (not always) a different order that what SQLITE Database Browser shows as column order in the Database Structure tab. So referencing the columns via getColumnIndex seems to be the only safe way.
I created the table using SQLiteOpenHelper the image of the table i created is given below
After this I delete the first row from the table and now my table looks like this
You can see that the id is now starting with 2.
So my problem is when I delete the first row the id first column goes to 2
Please help me to solve this issue . i want a table looks like this.
You can see that the id is now start with 2. So my problem is when I
delete the first row the id first column goes to 2 Please help me to
solve this issue
There is no problem with id value. If you are using autoincrement or not this is normal behaviour. In the case of autoincrement has internal iterator and it hasn't "memory".
So imagine this scenario: You have two rows with id 1, 2.
What will happen when you will delete first row?
In db will remain one row with id 2 but if you will insert next row, this row will be automatic have value id with 3 not deleted 1 also if you will delete first row, second row won't change value to 1.
In the other case if you are not using autoincrement this is possible(only make it programatically) but i think this approach is not effective. If you will have for example milion records, every row's id should be changed? This is sick.
So here is really not problem but normal behaviour.
Your issue is probably one of the following:
Your ID field is simply called id, whereas Android uses the conventional name _id (as a Java constant, BaseColumns._ID).
The ID field should be of type INTEGER PRIMARY KEY
I'm newbie in Android.
I want to display a new ID in the TextView.
So, I just think of getting latest ID that had been store in the database and declare as Integer add 1 to the value that I get then display to the TextView.
I have read many of the question regarding the getting the latest ID. How can I use select last_insert_rowid();?
Thanks!
last_insert_rowid() works only for records that have been inserted in the same session.
If your column is declared as INTEGER PRIMARY KEY, then SQLite will automatically generate a value for it if you don't specify one in a new record.
If you really need the ID before you have inserted the record, you can execute something like this:
SELECT max(_id) FROM MyTable
if you use autoincrement use
SELECT * from SQLITE_SEQUENCE;
to get the latest id.
Cursor c = database.rawQuery("SELECT last_insert_rowid()", null);
c.moveToFirst();
int id = c.getInt(0);
id += 1;
I'm a newbie too so can't explain very well. The above will get the last insert id from the same session. It won't work if a new session is started, ie you insert something and close the connection and reopen it, as it will then return 0 so you'll need to bear that in mind as your TextView would always show 1. As like you I read many questions about it without knowing how to implement it. The above code is how I managed to use it without getting outofbounds exceptions.
I have a table with 2 int type columns: one auto increment and primary, while the other is just not null.
now, whenever a record is inserted into the tale, the first auto-increment column gets a unique and new id. What I want is that this new ID value to be also inserted to the other column, in the same insert statement.
How can a achieve this? Plz help...
thnx in advance :)
I do not understand why you need in this? However, you can create the second column also with autoincrement (but you should check if the values in these two columns are the same). Moreover, you can create a trigger that will update the value of the second column after the insert (this is the best way, I guess).
I have a listview and i am getting the data from sqlite database. My problem is to delete a row which user selected it from listview. I can delete all table by
dbConn.delete("Restobj", null,null);
But i cant delete a single row which is selected from listview.
Please Help
You essentially need to get the row id from the selected ListView item. Using the row id you can easily delete that row:
String where = "_id = " + _id;
db.delete(TABLE_NAME, where, null);
After deleting the row item make sure you get the ListView adapter cursor and do a requery. If you don't you will not see the updated table, hence updated ListView.
Make use of those other two parameters to the delete method. Take a look at the API documentation for more information.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#delete%28java.lang.String,%20java.lang.String,%20java.lang.String[]%29
Pass in something other than null.
Also, try searching on stackoverflow and/or Google for this topic. The answers are plentiful.
You need to supply the appropriate values to the database object. I'm assuming that dbConn is an instance of a database object. If that is the case, you can pass in dbConn.delete() with 3 arguments. The first argument is the table name. The second is the whereClause. This is something similar to:
"id = " + objectDatabaseId
The final variable in this case you can leave blank. The end result is something like:
String whereClause = "id = " + objectDatabaseId;
dbConn.delete("Restobj", whereClause, null);
As a side note, it's better to use constants when referring to table names and table columns as apposed to "Restobj" you should have something like RestObject.TABLE_NAME where the constant is defined as a static final String inside of the RestObject.
-Randall