Updating SQLite - near "WHERE": syntax error (code 1 SQLITE_ERROR): - java

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+"'");

Related

SQLite Update Database Row in Android Studio Java

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.

Sqlite single column update query is not working

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 + "'");

Syntax Error: Encountered "RECURRING"

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.

Sqlite Database not updating using java

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

[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression

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;

Categories