When to call DB Errorhandling-Method?

Hi,

i use several database-methods with SQLite and MySQL. I also use a DBError-Method to catch the errors and to display a message to the user. This works almost fine, but i want to be sure:

where is the right place to call a db-error-method?

After which db-commands should my app check for an DBError?

Michael

every :slight_smile:

At least SQLSelect, SQLExecute and Prepare to catch the SQL syntax errors.

Hi Christian,

with “every” you mean every db-command in xojo? i don’t think so. Let take this code:


  dim dbFile as FolderItem
  
  dbFile = GetFolderItem("database").Child("database.sqlite")
  
  if dbFile <> Nil then
    
    SQLiteDB = new SQLiteDatabase
    SQLiteDB.DatabaseFile = dbFile
    
    if SQLiteDB.Connect then
      return
    else
      MsgBox("Datenbankfehler: " + SQLiteDB.ErrorMessage)
    end if
    
  end if

dim sql as String = "SELECT * FROM kunden ORDER BY firma COLLATE NOCASE"
    
    dim rs as RecordSet = SQLiteDB.SQLSelect(sql)
    
    while not rs.EOF
      Listbox1.AddRow
      Listbox1.RowTag(Listbox1.LastIndex) = rs.Field("ID").IntegerValue
      Listbox1.Cell(Listbox1.LastIndex, 0) = rs.Field("nummer").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 1) = rs.Field("firma").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 2) = rs.Field("telefon").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 3) = rs.Field("telefax").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 4) = rs.Field("email").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 5) = rs.Field("strasse").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 6) = rs.Field("hausnummer").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 7) = rs.Field("postleitzahl").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 8) = rs.Field("standort").StringValue
      Listbox1.Cell(Listbox1.LastIndex, 9) = rs.Field("land").StringValue
      rs.MoveNext
    wend
    
    if DBError then return
    
    rs.Close

After which commands you would place a “if DBError then return” which calls the Errorhandling-Method and returns True or False?

Michael

well, in your code, you need to put it at least after the SQLiteDB.SQLSelect.
rs could be nil. If one of the fields does not exist, you get a NilOBjectException there.

Personally I don’t use the close command there and let the recordset go out of scope.

Technically even the EOF or MoveNext could cause an error…

Thank you, Christian.

Michael