I have two databases with an identical structure: an SQLite DB and an MSSQL DB.
When I work with the SQLite database and want to read a record that does not exist, my app does not run into the “for each…” loop.
If I am working with the MSSQL database and want to read a record that does not exist, my app runs into the “for each…” loop.
In both cases the RecordSet “dbRows” is not nil but there are only columns with nil values in dbRows.
Could this be an error in ODBCDatabase?
My Code: DB is the SQLiteDatabase and DBo ist the ODBCDatabase
try
if DBo = nil then
dbRows = DB.SelectSQL("select * from "+DB_Table_Kunde+" where Kundennummer = '"+iv_kunnr.ToString+"'")
else
dbRows = DBo.SelectSQL("select * from "+DB_Table_Kunde+" where Kundennummer = '"+iv_kunnr.ToString+"'")
end if
if dbRows <> nil then
gs_py_syst.subrc = 4
for each row as DatabaseRow in dbRows
PufferKunde.Kdnr = row.Column("Kundennummer").IntegerValue
PufferKunde.KundenID = row.Column("KundenID").StringValue
PufferKunde.ErzeugtAm = row.Column("ErzeugtAm").StringValue
PufferKunde.Name = row.Column("Name").StringValue
PufferKunde.Vorname = row.Column("Vorname").StringValue
ODBCDatabase may behave a little bit different, because it depends on the data returned by the driver.
It might be a good idea to implementt a check for RowSet.RowCount > 0, if your ODBC Driver does support this.
I have implemented your suggestion as a workaround.
// >>>>> begin of workaround
var lv_RowCount as integer = 0
while not dbRows.AfterLastRow
lv_RowCount = lv_RowCount + 1
dbRows.MoveToNextRow
wend
if lv_RowCount = 0 then
return
end if
// <<<<< end of workaround
If this silently fails for ODBC connections, such construct should not be allowed, and cause a compiler error if possible, if not possible, raise a runtime exception “Operation not allowed: ODBC rows iterator”
Maybe it can be just fixed and work as expected, because I’m almost sure that behind the scenes that’s just some bug in the Iterator interface of RowSet.