getting exception in updating data base sqlite in android - android

to update a row in data base when i press a button
public void updateContact(long id, String x, String y,String angle)
{
ContentValues editCon = new ContentValues();
editCon.put(KEY_X, x);
editCon.put(KEY_Y, y);
editCon.put(KEY_ANGLE, angle);
ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + id, null);
}
and i use it entry.updateContact(1, Double.toString(db1), Double.toString(db2), Double.toString(db3));
but i get the exception: no such colum: _id1 (code1): , while compiling: UPDATE SavedTable SET position_y=?, position_x=?, position_angle=? WHERE_id1
even though i have a database with one row ( i create the entry in my oncreate method of main activity)

KEY_ROWID + id will not work as you expect! KEY_ROWID is probably a String containing _id and your id variable contains 1 which will result in _id1.
You need this:
ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + "=" + id, null);
or better:
ourDatabase.update(DATABASE_TABLE, editCon, KEY_ROWID + "=?", new String[]{String.valueOf(id)});
EDIT:
// pseudocode
Cursor c = ourDatabase.query(DATABASE_TABLE, null, null....);
if (c != null) {
if (c.getCount() >= 1) {
// there is an entry - just update
} else {
// no entry - create entry
}
} else {
// error? But definitively no entry so create one
}

You need to fix your update() method:
db.update(DATABASE_TABLE, editCon, KEY_ROWID + " = ?",
new String[] {String.valueOf(id)});
Problem was that you specified incorrect WHERE clause. You wrote this:
KEY_ROWID + id
this is translated on the database layer as _id1 where it was interpreted as column.
You needed _id = 1 and this is achieved by KEY_ROWID + " = ?" where placeholder will be replaced with value from string array.

Related

Android db.update where string

I'm using the method db.update to update the data according to the id.
now I would like at the same time change the same data that is present in another table. but the second part of the code does not work .... you have any ideas?
cv.put(CategorieTable.NOME_CATEGORIA, Ecatgoria.getText().toString());
String idc = id.getText().toString();
SQLiteDatabase db = mHelper.getWritableDatabase();
db.update(CategorieTable.TABLE_NAME, cv, idc + "=" + CategorieTable._ID, null);
//THE SECOND PART
cv.put(GiornateTable.CATEGORIA, Ecatgoria.getText().toString());
String nCategoria = Ecatgoria.getText().toString();
db.update(GiornateTable.TABLE_NAME, cv, nCategoria + "=" + GiornateTable.CATEGORIA, null);
You are using the same ContentValues instance (cv) for both operations. At second part you need to call cv.clear(), before put the new values.
cv.put(CategorieTable.NOME_CATEGORIA, Ecatgoria.getText().toString());
String idc = id.getText().toString();
SQLiteDatabase db = mHelper.getWritableDatabase();
db.update(CategorieTable.TABLE_NAME, cv, idc + "=" + CategorieTable._ID, null);
//THE SECOND PART
cv.clear(); // Clean the Content Values.
cv.put(GiornateTable.CATEGORIA, Ecatgoria.getText().toString());
String nCategoria = Ecatgoria.getText().toString();
db.update(GiornateTable.TABLE_NAME, cv, nCategoria + "=" + GiornateTable.CATEGORIA, null);

how to check if a value already exist in db, and if so how to get the id of that row? android sql

i have created the next db file -
String sql = ""
+ "CREATE TABLE "+ Constants.TABLE_NAME + " ("
+ Constants.NAME_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ Constants.NAME_PERSON + " TEXT"
+ ")";
db.execSQL(sql);
Now what I would like to know is, how to be able to run on the db and to know if a name already exist sin the db, and if so i would like to get the id of that row.
all i can understand is that i should use the
Cursor c= db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
but I don't have a clue what I should do next -
so thanks for any kind of help
you can add this in your DB and call the function passing "to be searched key" as an argument
public boolean checkIfExist(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_INFO, new String[] { KEY_TITLE}, KEY_TITLE + "=?",
new String[] { name }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
if (cursor.moveToFirst()) {
return true;
}else{
return false;
}
}
Where KEY_TITLE is the column name in your table.
Take more example on this:
AndroidSQLite
AndroidSQLite with multi tables
Make a SELECT request. Then check with if(cursor.moveToFirst()) if your name is already existing. (moveToFirst() return true if there is at least 1 value).
So if your value is existing, juste get its id with cursor.getString(cursor.getColumnIndex("_id"));

Issue deleting row from database

So this seems pretty simple but I haven't been able to figure out what I'm doing wrong here. I have my database delete command:
public boolean deleteContact(String rowId) {
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
I also should mention that I am displaying the database in a listview. So when the user clicks on an item they are presented with a "OK" and a "Delete" option.
When they hit delete this is the command I'm using:
db.open();
db.deleteContact("Apple Recipe");
db.close();
dialog.dismiss();
I keep getting a force close issue that says:
android.database.sqlite.SQLiteException: ner "Recipe":syntax error
(code 1): , while compiling: DELETE FROM list WHERE item=Apple Recipe
I'm probably doing something stupid so maybe fresh eyes will help.
Add ' in your delete statement, like this :
return db.delete(DATABASE_TABLE, KEY_ROWID + "='" + rowId + "'", null) > 0;
Or, as mentionned by other posters, you can use separate parameters :
String whereString = KEY_ROWID + "= ?";
String[] whereArgs = new String[] { rowId };
db.delete(DATABASE_TABLE, whereString, whereArgs);
the delete method for a contentResolver takes, a Uri, a "where" string, and an args string.
and row id is most likely an int
So it should be something like this
db.delete(DATABASE_TABLE,"rowId = ? ", row_id_number);
or if you are looking to match some field such as "recipe" which is a string then.
assuming the field name is recipe:
db.delete(DATABASE_TABLE, "recipe = ?", "Apple Recipe");
To be honest you should be using:
return db.delete(DATABASE_TABLE, KEY_ROWID + "=?", new String[]{rowId}) > 0;
Its more of a safety thing to do with escaping the values to make sure no SQL is in the string that could execute

sqlite update a tuple

I was trying to update a row in sqlite db, but it gives syntax error. I think I'm missing something but I can't see it. Maybe you can help me.
Update code:
public void updateEntry(String oldname,String name,String mail, String phone,String adres)
{
ContentValues args = new ContentValues();
args.put(COLUMN_NAME, name);
args.put(COLUMN_EMAIL, mail);
args.put(COLUMN_PHONE, phone);
args.put(COLUMN_ADDRESS, adres);
sqLiteDatabase.update(MYDATABASE_TABLE, args,COLUMN_NAME + "=" + oldname, null);
}
Calling update (By the way t,t2,t3,t4 are EditTexts)
mySQLiteAdapter.updateEntry(isim, t.getText().toString(), t2.getText().toString(), t3.getText().toString(), t4.getText().toString());
Errors
07-30 11:34:56.487: E/AndroidRuntime(1990): android.database.sqlite.SQLiteException: near "smith": syntax error: , while compiling: UPDATE MY_TABLE SET Name=?, Phone=?, Email=?, Address=? WHERE Name=john smith
First I thought, I didn't give any address or phone as string, but I printed them and I see the correct values. Any idea where my fault is?
It is because you have not escaped oldname string which contains john smith.
use sqLiteDatabase.update(MYDATABASE_TABLE, args,COLUMN_NAME + "='" + oldname + "'", null);
or use
String whereClause = COLUMN_NAME + "=?";
String[] whereArgs = new String[] {oldname};
sqLiteDatabase.update(MYDATABASE_TABLE, args, whereClause, whereArgs, null);

how to use whereClause, whereArgs when updating a table in sqlite?

In my android application i have tried to update a table using
sqliteDatabase.updateWithOnConflict(table, values, whereClause, whereClause, conflictAlgorithm)
method but i have no clear idea about whereClause and whereClause variables.following code will not give any exception or error but the table will not be updated.
AndroidOpenDbHelper androidOpenDbHelper = new AndroidOpenDbHelper(CreateListsActivity.this);
SQLiteDatabase sqliteDatabase = androidOpenDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(AndroidOpenDbHelper.LIST_NAME, editedKeyword);
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_LISTS, values, AndroidOpenDbHelper.LIST_NAME + "=" + id,null, SQLiteDatabase.CONFLICT_IGNORE);
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_KEYWORDS, values, AndroidOpenDbHelper.LIST_NAME + "=" + id, null, SQLiteDatabase.CONFLICT_IGNORE);
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_TWEET, values, AndroidOpenDbHelper.LIST_NAME + "=" + id, null, SQLiteDatabase.CONFLICT_IGNORE);
sqliteDatabase.close();
any suggestion??
Your conflict algorithm is IGNORE -
"When a constraint violation occurs, the one row that contains the constraint violation is not inserted or changed"
Does AndroidOpenDbHelper.LIST_NAME have a unique constraint on it which you may be violating? Try FAIL and check the return code:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#CONFLICT_FAIL
Also, bind the parameters in the statement. Thats the correct way to use where clause and args -
sqliteDatabase.updateWithOnConflict(AndroidOpenDbHelper.TABLE_NAME_KEYWORDS, values, AndroidOpenDbHelper.LIST_NAME + "= ?" , new String[]{id}, SQLiteDatabase.CONFLICT_IGNORE);
you can write your own sql update statement like this:
String sql="update <tableName> set <columnName> = 'newValue' where <columnName>= 'oldValue' ";
sqliteDatabase.execSql(sql,null);

Categories

Resources