marcick Well-Known Member Licensed User Longtime User Feb 1, 2016 #1 I suppose I'm doing something of wrong but this i the problem: StoDate is a text field and represent a date in the format "2016-02-01 12:54:00" If I use this code Code: Copy Dim rs As ResultSet = MySub.SQL_Bls1.ExecQuery("SELECT * FROM records WHERE V_ID = 2587 ORDER BY StoDate DESC LIMIT 1") Do While rs.NextRow Log(">>> " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop I obtain the expected result, the most recent record >>> 2587 - 2016-01-27 08:50:30 If I use this code instead Code: Copy Dim rs As ResultSet=MySub.SQL_Bls1.ExecQuery2("SELECT * FROM records WHERE V_ID = ? ORDER BY ? DESC LIMIT 1", Array("2587", "StoDate")) Do While rs.NextRow Log("*** " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop I obtain the oldest record *** 2587 - 2016-01-07 06:46:59 In other words the "order by" clause is ignored Where is my fault ?
I suppose I'm doing something of wrong but this i the problem: StoDate is a text field and represent a date in the format "2016-02-01 12:54:00" If I use this code Code: Copy Dim rs As ResultSet = MySub.SQL_Bls1.ExecQuery("SELECT * FROM records WHERE V_ID = 2587 ORDER BY StoDate DESC LIMIT 1") Do While rs.NextRow Log(">>> " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop I obtain the expected result, the most recent record >>> 2587 - 2016-01-27 08:50:30 If I use this code instead Code: Copy Dim rs As ResultSet=MySub.SQL_Bls1.ExecQuery2("SELECT * FROM records WHERE V_ID = ? ORDER BY ? DESC LIMIT 1", Array("2587", "StoDate")) Do While rs.NextRow Log("*** " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop I obtain the oldest record *** 2587 - 2016-01-07 06:46:59 In other words the "order by" clause is ignored Where is my fault ?
Erel B4X founder Staff member Licensed User Longtime User Feb 1, 2016 #2 1. Change "2587" with 2587. 2. The column name cannot be parameterized. Upvote 0
marcick Well-Known Member Licensed User Longtime User Feb 1, 2016 #3 Not really understood. Do you mean like this ? Code: Copy Dim rs As ResultSet=MySub.SQL_Bls1.ExecQuery2("SELECT * FROM records WHERE V_ID = ? ORDER BY ? DESC LIMIT 1", Array(2587, "StoDate")) Do While rs.NextRow Log("*** " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop The result is the same Upvote 0
Not really understood. Do you mean like this ? Code: Copy Dim rs As ResultSet=MySub.SQL_Bls1.ExecQuery2("SELECT * FROM records WHERE V_ID = ? ORDER BY ? DESC LIMIT 1", Array(2587, "StoDate")) Do While rs.NextRow Log("*** " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop The result is the same
Erel B4X founder Staff member Licensed User Longtime User Feb 1, 2016 #4 The second point meant that you cannot replace the column name with a question mark. Only values should be parameterized. Upvote 0
The second point meant that you cannot replace the column name with a question mark. Only values should be parameterized.
marcick Well-Known Member Licensed User Longtime User Feb 1, 2016 #5 Ah ..... yes, now it works Code: Copy Dim rs As ResultSet=MySub.SQL_Bls1.ExecQuery2("SELECT * FROM records WHERE V_ID = ? ORDER BY StoDate DESC LIMIT 1", Array(2587)) Do While rs.NextRow Log("*** " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop Great Erel Upvote 0
Ah ..... yes, now it works Code: Copy Dim rs As ResultSet=MySub.SQL_Bls1.ExecQuery2("SELECT * FROM records WHERE V_ID = ? ORDER BY StoDate DESC LIMIT 1", Array(2587)) Do While rs.NextRow Log("*** " & rs.GetString("v_id") & " - " & rs.GetString("StoDate")) Loop Great Erel