my sql delete command makes my db.insert to cause a crash - android

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 + " = ?"

Related

Count SQLite Android how to display cursor properly

I have a simple function with SQLite raw query, it looks like :
public Cursor getOccurations ()
{
SQLiteDatabase db =this.getWritableDatabase();
Cursor data= db.rawQuery("select count(*) from Beacon_occurations where BEACON_ADDRESS='" + COL_2 + "' and TIME='" + COL_3 +"'", null);
return data;
}
And now here is a question, how should I display it properly ? When I try do something like :
data = db.getOccurations();
string = data.toString();
Log.e("Message",string);
I get : Message: android.database.sqlite.SQLiteCursor#1a81ae97 ofc it isn't data which I want get :)
How should I do it properly ?
For such debug logging purposes, have a look at DatabaseUtils#dumpCursorToString().
For non-debug purposes, call getString() et al. on the cursor.
try this:
while(data.moveToNext()){
String s = data.getString(data.getColumnIndex("column Name"));
}
or
while(data.moveToNext()){
String s = data.getString(0);
}
Reading a single value from a query is easier with a helper function, such as longForQuery() or stringForQuery():
public long getOccurations()
{
SQLiteDatabase db = getWritableDatabase();
return DatabaseUtils.longForQuery(db,
"select count(*) from Beacon_occurations where BEACON_ADDRESS=? and TIME=?",
new String[]{ COL_2, COL_3 });
}
But to count rows, there is an even easier helper function, queryNumEntries():
public long getOccurations()
{
SQLiteDatabase db = getWritableDatabase();
return DatabaseUtils.queryNumEntries(
db, "Beacon_occurations",
"BEACON_ADDRESS=? and TIME=?",
new String[]{ COL_2, COL_3 });
}

SQL Lite DELETE Query not working

SQLite delete query isn't working. Can anyone tell me how to use db.delete(table, whereClause, whereArgs) method in limit query?!
String query = "DELETE From gpsinfo LIMIT 100 ";
Cursor cursor = db.rawQuery(query, null);
Try this...
Delete from table_name where ID IN (Select ID from table_name limit 100 );
Use execSQL() for SQL like this.
rawQuery() doesn't run the SQL until you move the returned cursor.
Also, you cannot use LIMIT like this.
String query = "DELETE FROM " +TABLE_NAME+ " WHERE " + COLUMN_NAME+ " = " + "'"+text+"'" ;
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(query);
db.close();

How to delete a specific row in sqlite databse in android?

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

Android SQLiteDatabase.delete method wouldn't delete using where args

Watch how i call the db.delete method; if I do the following a record is successfully deleted:
public void deteleProfile(Long id) throws SQLException {
SQLiteDatabase db = helper.getWritableDatabase();
Integer i = db.delete(ProlificDatabase.TABLE, "_id=?", new String[] {id.toString()});
Log.d(TAG, i + " records deleted where id is " + id);
however if I do this:
public void deteleProfile(Long id) throws SQLException {
SQLiteDatabase db = helper.getWritableDatabase();
Integer i = db.delete(ProlificDatabase.TABLE, "?=?", new String[] {BaseColumns._ID,id.toString()});
Log.d(TAG, i + " records deleted where id is " + id);
no records are deleted. also no exceptions or warnings are raised to say something has gone wrong.
in case you didn't catch it, the difference between the two calls were:
..."_id=?", new String[] {id.toString()});
vs
..."?=?", new String[] {BaseColumns._ID,id.toString()});
documentation for BaseColumns._ID is:
public static final String _ID with a Constant Value: "_id"
The latter way seems to make for neater code, but why doesn't it work?
EDIT:
I suspect the whereargs parameter only applies to the right side of an expression.
Can someone confirm this?
the Strings you provide in whereArgs are escaped and enclosed in '
"?=?", new String[] {BaseColumns._ID,id.toString()});
translates to
'_id'='1234'
which is valid SQLite syntax but does string comparison instead of a table lookup. Since "_id" is not the same String as "1234" (or any other number) the expression will always evaluate to false and nothing will get deleted.
What you should use is the following syntax
Integer i = db.delete(ProlificDatabase.TABLE, BaseColumns._ID + "=?", new String[] {id.toString()});
public void deteleProfile(Long id) throws SQLException
{
SQLiteDatabase db = helper.getWritableDatabase();
Integer i = db.delete(ProlificDatabase.TABLE, "_id=" + id, null);
Log.d(TAG, i + " records deleted where id is " + id);
}
and this link is a good example of SQLite database of android.

update sql database with ContentValues and the update-method

I would like to update my SQL lite database with the native update-method of the SQLiteDatabase class of android.
ContentValues dataToInsert = new ContentValues();
dataToInsert.put("name", "flo");
dataToInsert.put("location", "flotown");
String where = "id" + "=" + id;
try{
db.update(DATABASE_TABLE, dataToInsert, where, null);
}
catch (Exception e){
String error = e.getMessage().toString();
}
but I get following error:
android.database.sqlite.SQLiteException: near "15": syntax error: ,
while compiling: UPDATE mytable SET location=?, name=? WHERE
id=2010-09-21 15:05:36.995
I don´t know what should be the problem. Somehow the values do not arrive in the SQL statement. I did nearly the same with the insert method and that worked quite fine.
You're using the update function wrong. It should be like this:
String where = "id=?";
String[] whereArgs = new String[] {String.valueOf(id)};
db.update(DATABASE_TABLE, dataToInsert, where, whereArgs);
The Strings in the whereArgs array gets substituted in for each '?' in the where variable.
ie. if you had where = "name=? AND type=? then the first '?' would get replaced by whereArgs[0] and the second by whereArgs[1].
Actually, you just need to add apostrophes to your where clause. So it ought to be:
String where = "id='" + id + "'"
(note: however, this is not best practice, as it theoretically leaves open to injection attacks)
I have an other approach
public boolean updateEmployee(TalebeDataUser fav) {
SQLiteDatabase database = dbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DBHelper.COLUMN_ID, fav.getId());
contentValues.put(DBHelper.COLUM_AD, fav.getAd());
contentValues.put(DBHelper.COLUMN_NUMARA, fav.getNumara());
contentValues.put(DBHelper.COLUMN_YURD_ID, fav.getYurtID());
contentValues.put(DBHelper.COLUMN_EGITIM_ID, fav.getEgitimTur());
contentValues.put(DBHelper.COLUMN_TEL, fav.getTel());
contentValues.put(DBHelper.COLUMN_EMAIL, fav.getEmail());
contentValues.put(DBHelper.COLUMN_ADDRESS, fav.getAdres());
String whereClause = DBHelper.COLUM_AD + " = ? AND " + DBHelper.COLUMN_NUMARA + " = ? ";
final String whereArgs[] = {fav.getAd(), String.valueOf(fav.getNumara())};// old nameler taranıyor
int affectedRows = database.update(DBHelper.TABLE_NAME_OGR, contentValues, whereClause, whereArgs);
return affectedRows > 0;
}
Actually what exactly you written is correct. The syntax is correct.
But you have to check these.
String where = "id" + "=" + id;
In the above declaration "id" should be type number and id should be int.
And if id is a type of TEXT then follow #Adam javin answer.

Categories

Resources