Kewin Björk Nielsen Kewin Björk Nielsen - 4 months ago 7
Android Question

exception when trying to update records in the database

what i am trying to do is updating the records where it will search for the itemnumber and use is id to change the shelfnumber in another table based on what is in the edittext

this is the error i get when i tries to update the record


java.sql.SQLException: Incorrect syntax near the keyword 'file'.


and that happends at my public class and on my
preparedStatement.executeUpdate();


public class UpdatePro extends AsyncTask<String, String, String> {
String z = "";
Boolean isSuccess = false;
String item = cardnumberbox.getText().toString();
String shelf = shelfnumberbox.getText().toString();

@Override
protected String doInBackground(String... params) {

if (item.trim().equals("") || shelf.trim().equals(""))
z = getString(R.string.Invalid_Credentials);
else {
try {
Connection con = connectionClass.CONN();
if (con == null) {
z = getString(R.string.Forbindelses_fejl);;
} else {
String text1 = item;
{
if(text1.substring(0,1).startsWith("K"))
{
text1 = text1.substring(1);
}
else
{
text1= text1 ;//.substring(0));
}
}
String text2 = shelf;
{
if(text2.substring(0,1).startsWith("R"))
{
text2 = text2.substring(1);
}
else
{
text2= text2 ;//.substring(0));
}
}
String query = "Update PS set ShelfNumber = "+text2+ " from file.ItemPart PS "+ " join file.item P on P.id = PS.id "+ " where P.ItemNumber = '"+text1 + "'";
PreparedStatement preparedStatement = con.prepareStatement(query);
preparedStatement.executeUpdate();

z = getString(R.string.Update_succes);

isSuccess = true;
}
} catch (Exception ex) {
isSuccess = false;
Log.e("YOUR_APP_LOG_TAG", "I got an error", ex);
z = getString(R.string.Exceptions);
}
}
return z;

Answer

Try :

public class UpdatePro extends AsyncTask<String, String, String> {
String z = "";
Boolean isSuccess = false;
String item = cardnumberbox.getText().toString();
String shelf = shelfnumberbox.getText().toString();      

@Override
protected String doInBackground(String... params) {

    if (item.trim().equals("") || shelf.trim().equals(""))
        z = getString(R.string.Invalid_Credentials);
    else {
        try {
            Connection con = connectionClass.CONN();
            if (con == null) {
                z = getString(R.string.Forbindelses_fejl);;
            } else {
                String text1 = item;
                {
                    if(text1.substring(0,1).startsWith("K"))
                    {
                        text1 =  text1.substring(1);
                    }
                    else
                    {
                        text1=  text1 ;//.substring(0));
                    }
                }
                String text2 = shelf;
                {
                    if(text2.substring(0,1).startsWith("R"))
                    {
                        text2 =  text2.substring(1);
                    }
                    else
                    {
                        text2=  text2 ;//.substring(0));
                    }
                }
                String query = "Update PS set ShelfNumber = "+text2+ " from [file].[ItemPart] PS "+ " join [file].[item] P on P.id = PS.id "+ " where P.ItemNumber = '"+text1 + "'";
               PreparedStatement preparedStatement = con.prepareStatement(query);
                preparedStatement.executeUpdate();

                z = getString(R.string.Update_succes);

                isSuccess = true;
            }
        } catch (Exception ex) {
            isSuccess = false;
            Log.e("YOUR_APP_LOG_TAG", "I got an error", ex);
            z = getString(R.string.Exceptions);
        }
    }
    return z;
Comments