turning SQLite outcome to integer - Android - android

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;
}

Related

Pass id int into query

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)});
}

Update Individual field in DB

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.

Update Database comment return 1 but there is no update

Well I am updating my database with this code:
ContentValues newValues = new ContentValues();
newValues.put("tamam", true);
SQLiteDatabase db = mContext.openOrCreateDatabase(DB_NAME,
Context.MODE_WORLD_WRITEABLE, null);
return db.update(channel, newValues, "id =" + id , null);
it returns 1;
this is my return code:
if(msoru.isSolved()!= true){
TestAdapter mDbHelper = new TestAdapter(getActivity());
mDbHelper.createDatabase();
mDbHelper.open();
int truefalse = mDbHelper.updatecheck(mNum2);
boolean truefalse2 = msoru.isSolved();
but if I try to read new value it shows old value. I think there is no change
At the end of the database update method I should update also the my arrayclass with this
myArrayclass.get(mContext).setsolved(id);
db.close();
at last it look like this:
public void updatecheck(int id) {
ContentValues newValues = new ContentValues();
int ok = 1;
String sutun = "tamam";
newValues.put(sutun, ok);
SQLiteDatabase db = mContext.openOrCreateDatabase(DB_NAME,
Context.MODE_PRIVATE, null);
db.update(tablename, newValues, "id = ?",
new String[] { String.valueOf(id) });
myArrayclass.get(mContext).setsolved(id);
db.close();
}

Android database not updating

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();
}

Android db.update SQLite

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 });
}

Categories

Resources