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.
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 + "'");
I was trying to write a SQL statement that updates my database with the new information given. When I run the program it gives me an error saying that it has encountered "RECURRING" at line 1, column 401. What does this mean? I've added to the database using this word before, so I don't understand what the issue is. Here's the code...
stmt.executeUpdate("UPDATE \"CUSTOMERS\" SET FIRSTNAME = " + "'" + customer.getFirstName() + "'" + ", LASTNAME = " + "'" + customer.getLastName() + "'" + ", EMAIL = " + "'" + customer.getEmail() + "'" + ", PHONE = " + "'" + customer.getPhone() + "'" + ", ADDRESS = " + "'" + customer.getAddress() + "'" + ", GROUPONNUMBER = " + "'" + customer.getGrouponNumber() + "'" + ", NOTES = " + "'" + customer.getNotes() + "'" + ", ONETIME = " + customer.getOneTime() + ", RECURRING = " + customer.getRecurring()+ ", NONRESPONSIVE = " + customer.getNonResponsive() + " WHERE FIRSTNAME = " + "'" + active.getFirstName() + "'" + " AND LASTNAME = " + "'" + active.getLastName() + "'" + " AND EMAIL = " + "'" + active.getEmail() + "'" + "AND PHONE = " + "'" + active.getPhone() + "'" + "AND ADDRESS = " + "'" + active.getAddress() + "'" + "AND GROUPONNUMBER = " + "'" + active.getGrouponNumber() + "'" + "AND NOTES = " + "'" + active.getNotes() + "'" + " AND ONETIME = " + active.getOneTime() + "AND RECURRING = " + active.getRecurring() + "AND GROUPON = " + active.getGroupon() + "AND NONRESPONSIVE = " + active.getNonResponsive());
I've haven't used SQL too much, so I don't really know what I'm doing to be honest.
I am trying to update my new database but for some reason after executing my Prepared Statement the data remains the same. It returns no errors or any exceptions. Please have a look at my code and see if you can help me. Thanks in advance.
try {
connect();
PreparedStatement update = con.prepareStatement("UPDATE generalInfo SET "
+ "site" + " = '"
+ siteTt + "' , "
+ "area" + " = '"
+ areaTt + "' , "
+ "unit" + " = '"
+ unitTt + "' , "
+ "unitName" + " = '"
+ unitNameTt + "' , "
+ "drawing" + " = '"
+ drawingTt + "' , "
+ "system" + " = '"
+ systemTt + "' , "
+ "stream" + " = '"
+ streamTt + "' , "
+ "product" + " = '"
+ productTt + "' , "
+ "equipmentLoc" + " = '"
+ equipLocTt + "' , "
+ "specificLoc" + " = '"
+ specificLocTt + "' , "
+ "camOperator" + " = '"
+ camTechTt + "' , "
+ "camSerial" + " = '"
+ camSerialTt + "' , "
+ "gasSurveyOperator" + " = '"
+ surveyTechTt + "' , "
+ "gasSurveySerial" + " = '"
+ surveySerialTt + "' , "
+ "equipmentDesc" + " = '"
+ equipDescTt + "' , "
+ "equipmentType" + " = '"
+ equipTypeTt + "' , "
+ "equipmentSize" + " = '"
+ equipSizeTt + "' , "
+ "equipmentID" + " = '"
+ equipIDTt + "' , "
+ "[Maintenance type]" + " = '"
+ repairTt + "' , "
+ "measurementPosition" + " = '"
+ sourceTt + "'"
+ " WHERE leakerID = " + Integer.parseInt(leakerIDCombo.getSelectedItem().toString())+";");
update.closeOnCompletion();
update.executeUpdate();
con.close();
System.out.println("saved");
} catch (SQLException ex) {
Logger.getLogger(RefineryData.class.getName()).log(Level.SEVERE, null, ex);
}
never forgett to commit your changes:
con.setAutoCommit(true);
update.closeOnCompletion();
update.executeUpdate();
con.close();
or
update.closeOnCompletion();
update.executeUpdate();
con.commit();
con.close();
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;