Reputation: 11
So... This is my code:
public static final String DATABASE_NAME = "MY_DATABASE";
public static final String DATABASE_TABLE = "MY_TABLE";
public static final int DATABASE_VERSION = 1;
public static final String KEY_CONTENT = "Content";
public static final String KEY_ID = "_id";
private static final String SCRIPT_CREATE_DATABASE = "create table "
+ DATABASE_TABLE + " (" + KEY_ID
+ " integer primary key, " + KEY_CONTENT
+ " text not null);";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c) { context = c; }
public int delete() {
return sqLiteDatabase.delete(DATABASE_TABLE, KEY_ID , null);
}
There is no mistake message in log. Only break when I run it in my phone. I've used a lot of solutions. It does not want to work((( Could anybody tell, how to delete the last element in my table?
Upvotes: 0
Views: 544
Reputation: 20346
Second parameter of SqliteDatabase.delete()
method is WHERE clause. You didn't pass to delete() that clause, didn't say what you want to delete. Basically you get this SQL query:
DELETE FROM DATABASE_TABLE WHERE KEY_ID;
@Alexandre Martin gave you several variants how to get last id.
Upvotes: 1
Reputation: 1502
Get the last inserted element with a Select query limited to one element and ordered by id DESC.
You could also loop throught all elements and override datas till the end, but is not that much efficient if your db has a lot of datas.
Finally, you can use
SELECT last_insert_rowid()
just after you insert something and keep the id as a class scoped variable.
https://www.sqlite.org/c3ref/last_insert_rowid.html
Upvotes: 2