I want to update a SQLite Database Row by using this code:
public void updateProfile(String profileName, String col3, String col4, String col5, String col6, String col7, String col8, String col9, String col10, String col11, String col12, String col13, String col14, String col15, String col16, String col17){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET "
+ COL3 + " = '" + col3
+ COL4 + " = '" + col4
+ COL5 + " = '" + col5
+ COL6 + " = '" + col6
+ COL7 + " = '" + col7
+ COL8 + " = '" + col8
+ COL9 + " = '" + col9
+ COL10 + " = '" + col10
+ COL11 + " = '" + col11
+ COL12 + " = '" + col12
+ COL13 + " = '" + col13
+ COL14 + " = '" + col14
+ COL15 + " = '" + col15
+ COL16 + " = '" + col16
+ COL17 + " = '" + col17
+ "' WHERE " + COL2 + " = '" + profileName + "'";
Log.d(TAG, "update Profile: " + profileName);
db.execSQL(query);
}
For some reason i get some errors.
You have two recurring mistakes/errors :-
you are omitting a closing single quote around the value that is being set, and
you are omitting the , (comma) between the individual set clauses.
You want :-
public int updateProfile(String profileName, String col3, String col4, String col5, String col6, String col7, String col8, String col9, String col10, String col11, String col12, String col13, String col14, String col15, String col16, String col17){
SQLiteDatabase db = this.getWritableDatabase();
String query = "UPDATE " + TABLE_NAME + " SET "
+ COL3 + " = '" + col3 + "',"
+ COL4 + " = '" + col4 + "',"
+ COL5 + " = '" + col5 + "',"
+ COL6 + " = '" + col6 + "',"
+ COL7 + " = '" + col7 + "',"
+ COL8 + " = '" + col8 + "',"
+ COL9 + " = '" + col9 + "',"
+ COL10 + " = '" + col10 + "',"
+ COL11 + " = '" + col11 + "',"
+ COL12 + " = '" + col12 + "',"
+ COL13 + " = '" + col13 + "',"
+ COL14 + " = '" + col14 + "',"
+ COL15 + " = '" + col15 + "',"
+ COL16 + " = '" + col16 + "',"
+ COL17 + " = '" + col17
+ "' WHERE " + COL2 + " = '" + profileName + "'";
Log.d(TAG, "update Profile: " + profileName);
db.execSQL(query);
}
Note the above is untested but in principle is what you want.
However;
simpler and less prone to errors is to let the SQLiteDatabase update convenience method do the work of generating the SQL for you. As such I'd suggest using :-
public void updateProfile(String profileName, String col3, String col4, String col5, String col6, String col7, String col8, String col9, String col10, String col11, String col12, String col13, String col14, String col15, String col16, String col17){
ContentValues cv = new ContentValues();
cv.put(COL3,col3);
cv.put(COL4,col4);
cv.put(COL5,col5);
cv.put(COL6,col6);
cv.put(COL7,col7);
cv.put(COL8,col8);
cv.put(COL9,col9);
cv.put(COL10,col10);
cv.put(COL11,col11);
cv.put(COL12,col12);
cv.put(COL13,col13);
cv.put(COL14,col14);
cv.put(COL15,col15);
cv.put(COL16,col16);
cv.put(COL17,col17);
SQLiteDatabase db = this.getWritableDatabase();
Log.d(TAG, "update Profile: " + profileName);
return db.update(TABLE_NAME,cv,COL2 + "=?",new String[]{profileName};
}
Again this code hasn't been tested or run so it may contain some errors.
Note the link to the update method above.
Note that public int updateProfile has been used, this means that the number of rows that have been updated will be returned, which may be useful.
the COL2 + "=?", is saying to bind the parameter, that is to replace the ? with the value as per the String[].
multiple ?'s can be used for more complex situations, in which case the first ? will be replaced by the first element in the String[], the second ? will be replaced by the 2nd element in the String[] and so on.
Related
I recently added a column to my SQLite database, since adding this column a button which I had which sets the value of one of the database columns to "1" or "0" have now started to crash my application when the button tries to use the updateData(), so I assume thats where my issue is, is in the code or the syntax of the updateData()
Error:
android.database.sqlite.SQLiteException: near "WHERE": syntax error
(code 1 SQLITE_ERROR): , while compiling: UPDATE my_manager SET
location_name = 'blarney stone' , location_county =
'Cork',location_description = 'jj',location_route =
'1',location_position = '2',location_longg = 'null',location_lat =
'null',location_url = 'JJ',location_url2 = 'jj',location_url3 = 'jj',
WHERE location_id = '1'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method)
Code:
void updateData(String id, String row_id, String name, String county, String description, String
in_route, String position, String lat, String longg, String url, String url2, String url3) {
System.out.println(TABLE_NAME+"; "+row_id +"; "+ name +"; "+ county+"; "+ description+"; " + lat
+"; "+ longg+"; "+ url +"; "+ url2 +"; "+ url3 +"; ");
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("UPDATE " + "my_manager" + " SET location_name = "+"'"+ name + "' " + ", " +
"location_county = " + "'"+ county + "'"+ "," +
"location_description = " + "'"+ description + "'" + "," +
"location_route = " + "'"+ in_route + "'" + "," +
"location_position = " + "'"+ position + "'" + "," +
"location_longg = " + "'"+ longg + "'" + "," +
"location_lat = " + "'"+ lat + "'" + "," +
"location_url = " + "'"+ url + "'" + "," +
"location_url2 = " + "'"+ url2 + "'" + "," +
"location_url3 = " + "'"+ url3 + "'" + "," + " WHERE location_id = "+"'"+ row_id+"'");
Sorry my friend don't take this as a bad but how about you avoid to much unnecessary "+" if it is possible.
void updateData("UPDATE my_manager SET location_name = '"+ name + "', " +
"location_county = '"+ county + "', " +
"location_description = '"+ description + "'," +
"location_route = '"+ in_route + "', " +
"location_position = '" + position + "', " +
"location_longg = '" + longg + "'," +
"location_lat = '" + lat + "'," +
"location_url = '" + url + "', " +
"location_url2 = '"+ url2 + "', " +
"location_url3 = '"+ url3 + "' WHERE location_id = '"+ row_id+"'");
I would like to know how to query a database based on the amount of mistakes made by a waiter.
The two tables I have are
Employee
String employeeTable = "CREATE TABLE " + EMP_TABLE + " ("
+ ID_EMP + " INTEGER PRIMARY KEY, "
+ FIRST_NAME + " TEXT,"
+ LAST_NAME + " TEXT,"
+ PROFIT + " INTEGER);";
MistakeEmployee
String mistakeTable = "CREATE TABLE " + MISTAKE_TABLE + " ("
+ ID_MISTAKE + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ID_WAITER_MISTAKE + " INTEGER,"
+ ID_ORDER_MISTAKE + " INTEGER,"
+ MISTAKE_TOTAL + " INITGER,"
+ " FOREIGN KEY (" + ID_WAITER_MISTAKE + ") REFERENCES " + EMP_TABLE + "(" + ID_EMP + "), "
+ " FOREIGN KEY (" + ID_ORDER_MISTAKE + ") REFERENCES " + ORDER_TABLE + "(" + ID_ORDER + "));";
I am trying to order the list based on the Employee with the most Mistakes which would be counted in the mistakes table using the ID_WAITER_MISTAKE.
String query = SELECT * , COUNT(Mistake_TABLE.ID_Waiter) as Count
FROM EMP_TABLE
ORDER BY Count DESC
You must join the tables, group by employee and aggregate:
SELECT e.ID_EMP, e.FIRST_NAME, e.LAST_NAME,
COUNT(m.ID_MISTAKE) AS Mistake_Count
FROM employeeTable e LEFT JOIN mistakeTable m
ON m.ID_WAITER_MISTAKE = e.ID_EMP
GROUP BY e.ID_EMP, e.FIRST_NAME, e.LAST_NAME
ORDER BY Mistake_Count DESC, e.ID_EMP
and in Java code you should construct the sql string like this:
String query =
"SELECT e." + ID_EMP + ", e." + FIRST_NAME + ", e." + LAST_NAME + ", " +
"COUNT(m." + ID_MISTAKE + ") AS Mistake_Count " +
"FROM " + EMP_TABLE + "AS e LEFT JOIN " + MISTAKE_TABLE + " AS m " +
"ON m." + m.ID_WAITER_MISTAKE + " = e." + ID_EMP + " " +
"GROUP BY e." + ID_EMP + ", e." + FIRST_NAME + ", e." + LAST_NAME + " " +
"ORDER BY Mistake_Count DESC, e." + ID_EMP ;
I hope there are no typos.
So, I am currently using the DragSortListView with a SQLite database. I want to resort my database whenever im dragging an item of the ListView to a new position. Therefore I wrote a piece of code that should manage it. But if I'm moving something from a position below to a higher one, it is strangely sorted. I already looked on this code for hours and asked some friends, but I never found the solution.
Code
Method in the DatabaseHelper
public void moveValues(int from, int to, String sammlung){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
System.out.println(from + " - " + to);
if(from < to){
//This part is working properly
Cursor cursor = sqLiteDatabase.rawQuery("SELECT ID FROM " + TABLE_NAME + " WHERE POSITION IS " + "\"" + from + "\" AND SAMMLUNG IS " + "\"" + sammlung + "\"", null);
for (int i = from+1; i < to+1; i++){
switchPositionValue(i, i-1);
}
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_5, to);
cursor.moveToFirst();
sqLiteDatabase.update(TABLE_NAME, contentValues, "ID=?", new String[]{cursor.getInt(0)+""});
cursor.close();
}else{
//This is the not working part
Cursor cursor = sqLiteDatabase.rawQuery("SELECT ID FROM " + TABLE_NAME + " WHERE POSITION IS " + "\"" + from + "\" AND SAMMLUNG IS " + "\"" + sammlung + "\"", null);
for (int i = from-1; i > to-1; i--){
switchPositionValue(i, i+1);
}
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_5, to);
cursor.moveToFirst();
System.out.println(cursor.getInt(0));
//The next line destroys it. But I dont know how to fix it or what exactly is not working
sqLiteDatabase.update(TABLE_NAME, contentValues, "ID=?", new String[]{cursor.getInt(0)+""});
cursor.close();
}
}
The parameters are:
from: The position it's taken away from
to: The position it's taken to
sammlung: I saving all the items of different collections in one database. So it's only there to sort out only specific items.
Method to change the value of a row with a specific position
public void switchPositionValue(int before, int after){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_5, after);
sqLiteDatabase.update(TABLE_NAME, contentValues, "POSITION=?", new String[]{before+""});
}
Demonstration
So if you've got any idea why its behaving this strange or do know a better approach to moving a row in a database, I would really appreciate your help.
Android other way SQL one (COLUMN_5 is POSITION column)
public void moveValues(int from, int to) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String query =
"WITH p(from_position,to_position,max_position,min_position) AS (" +
"SELECT " +
"?," +
"?," +
"(SELECT max(" + COLUMN_5 + ") FROM " + TABLE_NAME + ")," +
"(SELECT min(" + COLUMN_5 + ") FROM " + TABLE_NAME + ")), " +
"updates AS(" +
"SELECT " + TABLE_NAME + ".id," + COLUMN_5 + " AS current, " + COLUMN_5 + " + 1 AS proposed " +
"FROM " + TABLE_NAME + " " +
"WHERE " + COLUMN_5 + " >= (SELECT to_position FROM p) " +
"AND " + COLUMN_5 + " < (SELECT from_position FROM p) " +
"UNION SELECT " + TABLE_NAME + ".id," + COLUMN_5 + " AS current, " + COLUMN_5 + " -1 AS proposed " +
"FROM " + TABLE_NAME + " " +
"WHERE " + COLUMN_5 + " <= (SELECT to_position FROM p) " +
"AND " + COLUMN_5 + " > (SELECT from_position FROM p) " +
"UNION SELECT " + TABLE_NAME + ".id," + COLUMN_5 + ",(SELECT to_position FROM p) " +
"FROM " + TABLE_NAME + " " +
"WHERE " + COLUMN_5 + " = (SELECT from_position FROM p) " +
"AND (SELECT from_position FROM p) <> (SELECT to_position FROM p)" +
"), " +
"finish_updates AS (" +
"SELECT * FROM updates " +
"WHERE max((SELECT from_position FROM p),(SELECT to_position FROM p)) <= (SELECT max_position FROM p) " +
"AND min((SELECT from_position FROM p),(SELECT to_position FROM p)) >= (SELECT min_position FROM p)" +
")" +
"UPDATE " + TABLE_NAME + " SET " + COLUMN_5 + " = " +
"(" +
" SELECT proposed " +
" FROM finish_updates " +
" WHERE finish_updates.id = " + TABLE_NAME + ".id" +
") " +
"WHERE " + TABLE_NAME + ".id IN " +
"(" +
" SELECT id FROM finish_updates" +
");";
SQLiteStatement stmnt = sqLiteDatabase.compileStatement(query);
stmnt.bindLong(1,from);
stmnt.bindLong(2,to);
stmnt.executeUpdateDelete();
}
fiddle test SQL at
My UpdateRow function is not working. Can someone help?
public boolean UpdateRow(String date ,String timeOut) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("UPDATE " + TABLE_NAME + " SET " + COL_3 + "=" + timeOut + "WHERE " + COL_1 + "=" + date);
}
Thanks in advance.
I think the query that you have written is missing a space between WHERE clause and COL_1. The line should be written as follows.
db.execSQL("UPDATE " + TABLE_NAME
+ " SET " + COL_3 + " = " + timeOut
+ " WHERE " + COL_1 + " = " + date
);
Apostrophes around date and space between columns:
db.execSQL("UPDATE " + TABLE_NAME + " SET " + COL_3 + "=" + timeOut + " WHERE " + COL_1 + "='" + date + "'");
If timeOut is TEXT then it needs apostrophes too:
db.execSQL("UPDATE " + TABLE_NAME + " SET " + COL_3 + "='" + timeOut + "' WHERE " + COL_1 + "='" + date + "'");
Need help with this update query on JAVA, just started learning this but having problems
Getting following error upon execution
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing
operator) in query expression 'B.AWHERE ID =4'.
and data is not updating in MS Access database file
public void update(Student s)
{
int w = Integer.parseInt(s.getID());
String query = "UPDATE Student SET ID =" + w + "," + "FirstName =" + s.getFirstName() + "," + "LastName =" + s.getLastName() + "," + "Address =" + s.getAddress() + "," + "Gender =" + s.getGender() + "," + "DOB =" + s.getDOB() + "," + "Degree =" + s.getDegree() + "WHERE ID =" + w;
try
{
stmt.executeUpdate(query);
}
catch(SQLException e)
{
System.out.println("Problem in Query");
e.printStackTrace();
}
}
Change your UPDATE statement to be like below
String query = "UPDATE Student SET "FirstName = '" + s.getFirstName() +
"'," + "LastName = '" + s.getLastName() +
"'," + "Address = '" + s.getAddress() +
"'," + "Gender = '" + s.getGender() +
"'," + "DOB = '" + s.getDOB() +
"'," + "Degree = '" + s.getDegree() +
"' WHERE ID = " + w;
But your query won't make much sense cause, you are setting ID = w and also checking WHERE ID = w
You are missing spaces and apostrophes for string values in your whole query E.g.
"Degree =" + s.getDegree() + "WHERE ID ="
is being evaluated to
"Degree =BAWHERE ID ="
which is invalid syntax.
EDIT: I can only guess that your Student object returns strings in the getter methods so try this.
String query = "UPDATE Student SET ID =" + w + ","
+ "FirstName =\"" + s.getFirstName() + "\","
+ "LastName =\"" + s.getLastName() + "\","
+ "Address =\"" + s.getAddress() + "\","
+ "Gender =\"" + s.getGender() + "\","
+ "DOB =\"" + s.getDOB() + "\","
+ "Degree =\"" + s.getDegree() + "\" "
+ "WHERE ID =" + w;