I have a query where I pass by parameters the groupID, but the query does not recognize the number(group id).All the values are reaching the query( I debugged). This is my query, can you see if the structure is ok?
public Cursor updateGrupo(String nomegrupo,int idgroup){
SQLiteDatabase db = this.getReadableDatabase();
//Does not put the ID in the "idgroup"
Cursor c = db.rawQuery("UPDATE '"+tableGrupo+"' SET '"+nomeGrupo+"' = '"+nomegrupo+"' WHERE '"+idGrupo+"' = '"+idgroup+"'", new String[]{});
return c;
}
You should use SQLiteDatabase as writable mode getWritableDatabase().
You should use below method for updating data:
update(String table, ContentValues values, String whereClause, String[] whereArgs)
Try this:
public void updateGrupo(String nomegrupo,int idgroup) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(nomeGrupo, nomegrupo);
db.update(tableGrupo, values, idGrupo + " = ? " , new String[]{String.valueOf(idgroup)});
}
Related
I'm working on a project and in need to use new ID's added to my database as numbers,
How do I turn this:
String query = "SELECT * FROM " + playlistsDBName + " WHERE ID = LAST_INSERT_ROWID";
To an integer (or some other number type) that I can use in my Android code?
you should change this your method from:
public void addNewPlaylist(String DBName, String playlistName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("PlaylistName", playlistName);
db.insert(DBName, null, values);
db.close();
}
to
public int addNewPlaylist(String DBName, String playlistName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("PlaylistName", playlistName);
final int id = db.insert(DBName, null, values);
db.close();
return (int)id;
}
I am Creating DB with three fields name,id,mark. I created table and inserted values using
StudentDb.addStudentDetail(new StudentDetails(id,Name,0));
I used Update method Like:
public int updateStudentDetail(StudentDetails student) {
SQLiteDatabase db = this.getWritableDatabase();`enter code here`
ContentValues values = new ContentValues();
values.put("id", student.getID());
values.put("name", student.getName());
values.put("mark", student.getMark());
int i = db.update(TABLE_STUDENT, values, KEY_ID + " = ?",
new String[] { String.valueOf(student.getId()) });
db.close();
return i;
}
I call this method Like:
StudentDb.updateStudentDetail(student);
I don't know how to pass a value update method?
its very simple. Lets say you want to update the marks of a student. Implement this in your DatabaseHelper class:
public int udpateStudentMarks(final int studentID,final int marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("mark", marks);
int i = db.update(TABLE_STUDENT, values, KEY_ID + " = ?",
new String[] { String.valueOf(studentID) });
db.close();
return i;
}
now you can use it like this:
db.updateStudentMarks(studID,newMarks);
Hope it helps.
This method is supposed to update the friend in the database. But it is not. No error is shown, tried restarting to refresh the list etc still nothing. Log is showing me new values but they are not saved in the database.
public void updateFriend(Friend friend) {
Log.d("UpdateFriendMethod", friend.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, friend.getName());
values.put(KEY_BIRTHDAY, friend.getBirthday().getTimeInMillis());
db.update(TABLE_Friends,
values,
KEY_NAME + " = ?",
new String[] { String.valueOf(friend.getName()) });
db.close();
}
Here is the way how do I get items from db:
public List<Friend> getAllFriends() {
List<Friend> Friends = new LinkedList<Friend>();
String query = "SELECT * FROM " + TABLE_Friends;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
Friend friend = new Friend();
friend.setName(cursor.getString(0));
Date date = new Date(cursor.getLong(1));
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
friend.setBirthday(cal);
Friends.add(friend);
} while (cursor.moveToNext());
}
Log.d("getAllFriends()", Friends.toString());
return Friends;
}
I bet, problem is somewhere in that SQL statement, I am not familiar with it yet, I am used to SQL language not this.
SOLVED, problem was in SQL statement as I later found out:
public void updateFriend(Friend friend) {
Log.d("UpdateFriendMethod", friend.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, friend.getName());
values.put(KEY_BIRTHDAY, friend.getBirthday().getTimeInMillis());
db.update(TABLE_Friends, values, KEY_NAME + "='" + friend.getName()
+ "'", null);
db.close();
}
I have a small problem with the method db.update. I need to change a string that corresponds to that received by the query. For example from the query I get the string "hello", if the change in "hello1" must change all the strings "hello".
In my Cursor I have name_s = c.getString(3);
And this is my update:
cv.put(Table1.ABC, Ecia.getText().toString());
db.update(Table1.TABLE_NAME, cv, Table1.ABC+ " = ?", new String[] { name_s});
try this :
String newval=Ecia.getText().toString();
String name_s = c.getString(3);
setMyField(name_s , newval);
public int setMyField(String currvalue , String newvalue) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Table1.ABC, newvalue);
// updating row
return db.update(Table1.TABLE_NAME, values, Table1.ABC + " = ?",
new String[] { currvalue });
}
When I use the getPesoCount() like in the:
Log.v("SQL", String.valueOf(getPesoCount()));
I get aN error:
FATAL EXCEPTION: main
java.lang.IllegalStateException: attemp to re-open an already-closed object: android.database.sqlite.SQLiteQuery(mSql = SELECT * FROM pesoTable)...
public void addPeso(int peso, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PESO, peso); // Contact Name
values.put(KEY_DATE, date); // Contact Phone Number
// Inserting Row
db.insert(TABLE_PESO, null, values);
Log.v("SQL", String.valueOf(getPesoCount()));
db.close(); // Closing database connection
}
public int getPesoCount() {
String countQuery = "SELECT * FROM " + TABLE_PESO;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
If I don't use getPesoCount(), no error occurs.
Can someone help me?
attempt to re-open an already-closed object:
Exception tells you a lot!
If you want to use the cursor further do not call cursor.close(); call it when you are sure that you do not need it anymore.
public void addPeso(int peso, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_PESO, peso); // Contact Name
values.put(KEY_DATE, date); // Contact Phone Number
// Inserting Row
db.insert(TABLE_PESO, null, values);
Log.v("SQL", String.valueOf(getPesoCount(db)));
db.close(); // Closing database connection
}
public int getPesoCount(db) {
String countQuery = "SELECT * FROM " + TABLE_PESO;
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
return count;
}