How to insert value in Oracle DB with right syntax - java

I wrote a query
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
"VALUES('" + VAR1+ "','" + VAR2 + "','" + VAR3 + "','" + " '" + VAR4 + "','" + "VAR5 + );";
where var5 is a string.
When I try to insert into table and use above SQL I get the error
java.sql.SQLSyntaxErrorException: ORA-00917 : no comma
Where is the mistake?

You had the quotes wrong:
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
"VALUES('" + VAR1 + "','" + VAR2 + "','" + VAR3 + "','" + VAR4 + "','" + VAR5 + ");";
The quote next to last must be placed between the plus and the parenthesis, not before VAR5. Also you doubled the single quote before VAR4 (credit to DevilsHnd).

Your single and double quotes around VAR5 was not correct and also you don't need ; in the string:
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
"VALUES('" + VAR1+ "','" + VAR2 + "','" + VAR3 + "','" + " '" + VAR4 + "','" + VAR5 + "')";

Thanks for your help
The right syntax is:
sql = "INSERT INTO TABLE(COLUMN1, COLUMN2, COLUMN3, COLUMN4, COLUMN5) " +
" VALUES('" + VAR1+ "','" + VAR2+ "','" + VAR3+ "','" + VAR4+ "','" + VAR5+ "')";

Related

SQLite query for ordering a list based on mistakes made

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.

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.

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

Update Query in GRN

This is a stock management system and I have a problem with updating part in good received note. When we purchase goods, we get a good received note, so when we get the items the number of items has to be updated (has to be added to the remaining goods) according to the item code.
According to the following code data from GRN will be extracted and will be saved in GRN1 and GRN2 tables. There's another table called Item_Supplier, when we receive goods Stock in hand field in Item_Supplier table has to updated according to the item_Id as well.
Problem is following code does not work.
Connection ss = Class_DB.myconnection();
Statement st = ss.createStatement();
-
ResultSet rs = st.executeQuery("select stock_in_hand from item_supplier where item_ID =('" + TF_GRN_ITEMID.getText() + "')");
grniid = TF_GRN_NO_OF_ITEM.getText();
while (rs.next()) {
nit = rs.getString("stock_in_hand");
}
sumn = grniid + nit;
st.executeUpdate("insert into grn1 values('" + TF_GRN_GRNNO.getText() + "','" + TF_GRN_SUPPLIERID.getText() + "','" + TF_GRN_AMOUNT.getText() + "','" + TF_GRN_DATE.getText() + "')");
st.executeUpdate("insert into grn2 values('" + TF_GRN_GRNNO.getText() + "','" + TF_GRN_ITEMID.getText() + "','" + TF_GRN_EXP_DATE.getText() + "','" + TF_GRN_TAX.getText() + "','" + TF_GRN_NO_OF_ITEM.getText() + "','" + TF_GRN_GAMOUNT.getText() + "','" + TF_GRN_NAMOUNT.getText() + "','" + TF_GRN_QTY.getText() + "','" + TF_GRN_UNIT.getText() + "','" + TF_GRN_FREE.getText() + "','" + TF_GRN_DIS.getText() + "')");
st.executeUpdate("update item_supplier set stock_in_hand='" + sumn + "' where item_ID='" + TF_GRN_ITEMID.getText() + "'");
JOptionPane.showMessageDialog(null, "Data Saved");
Please help.

Having Syntax Error in code java

Why is there a syntax error in this code?
String strSqlUpdate = "UPDATE Customers SET Contact = " + contact_num + ","
+ "Email = '" + email_add + "',"
+ "Address = '" + mail_add + "',"
+ "SurveyStatus = " + radio_group + ","
+ "Subscription = " + receive_info +
"WHERE membership_ID = '" + member_ID';
I thought my code was right.
If it is the error in your code, check all the variables that you have used are declared and initialized with proper values.
If it is the syntax of the sql that is bothering you , here is what your sql would look like if all the variables are initialized to null.
UPDATE Customers SET (Contact)null,Emailnull,Address,null,SurveyStatus,null,SubscriptionnullWHERE MembershipID =null
Use spaces in your strSqlUpdate to correct the above sql.
EDIT
What you need is something like this.
String strSqlUpdate = "UPDATE Customers SET Contact = " + contact_num
+ ",Email = '" + email_add + "'"
+ ",Address = '" + mail_add + "'"
+ ",SurveyStatus = '" + radio_group + "'"
+ ",Subscription = '" + receive_info + "' "
+ "WHERE membership_ID = '" + member_ID + "'";
I get no syntax errors when I declare and Initialize all of the variables. You have to make sure they're all initialized, within the scope of the strSqlUpdate
String contact_num = "";
String email_add = "";
String mail_add = "";
String radio_group = "";
String receive_info = "";
String member_ID = "";
String strSqlUpdate = " UPDATE Customers SET (Contact)" + contact_num + "," + "Email"
+ email_add + "," + "Address" + "," + mail_add + "," + "SurveyStatus" + "," + radio_group
+ "," + "Subscription" + receive_info + "WHERE MembershipID =" + member_ID;
Also considering you're talking about SQL syntax, adding on to what others have said, I'd advise you should use a PreparedStatement to avoid SQL injection.
PreparedStatement pst = conn.prepareStatement(
"UPDATE Customers SET (Contact) ?, ?, ?, ?, ?, ?, ? WHERE ? = ?");
pst.setString(1, contact_num);
pst.setString(2, email_add);
... and so on
An error in your current SQL syntax is this
"Subscription" + receive_info + "WHERE MembershipID
Translated as
"...Subscrptionreceive_infoWHERE MembershipID..."
You need to add spaces wherever you don't have commas

Categories