When are exceptions used and when errors for SQLite? I thought that only exceptions were to be used anymore. But I see a mix of exceptions and errors. In the following code I’m making a simple database and add one row of data:
Var dbFile As New FolderItem("MyDB.sqlite")
Var db As SQLiteDatabase
db = New SQLiteDatabase
db.DatabaseFile = dbFile
Try
call db.CreateDatabaseFile
Dim sql As String = "CREATE TABLE ""AccountsAndMailClients""(""AccountID"" Text PRIMARY KEY, ""Accountname"" Text NOT NULL, ""Username"" Text, ""Servername"" Text, ""Port"" Integer, ""Authentication"" Text, ""UseSSL"" Boolean, ""IsIMAP"" Boolean NOT NULL)"
db.ExecuteSQL(sql)
Catch error As DatabaseException
MessageBox("Database error: " + error.Message)
End Try
try
dim theSQL as String= "INSERT INTO AccountsAndMailClients(AccountID, AccountName, ServerName, IsIMAP, Port, Authentication, UseSSL) VALUES(?, ?, ?, 1, 993, 'Password' true)"
dim thePreparedStatement as SQLitePreparedStatement = db.Prepare(theSQL)
thePreparedStatement.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
thePreparedStatement.BindType(1, SQLitePreparedStatement.SQLITE_TEXT)
thePreparedStatement.BindType(2, SQLitePreparedStatement.SQLITE_TEXT)
thePreparedStatement.SQLExecute("AccountID", "AccountName", "ServerName")
catch err as DatabaseException
MessageBox("Database error: " + err.Message)
end try
It took me quite a while to see that I missed a comma in the INSERT. The exception is never reached. The error is set instead:
Bug or feature? Unclear. The docs state that an error in the SQL will cause the Prepare statement to throw a Database Error so what you are seeing is, technically, correct. Whether it should be seen as an exception is another issue.
Thanks, Wayne, to my eyes ExecuteSQL and SQLExecute look the same. According to the docs SQLExecute is deprecated. But I don’t see that after doing a Cmd-K. Made a bug report.
I believe Beatrix meant that it is difficult to realize that there is a difference in either using ExecuteSQL or SQLExecute and remembering what one should be used for. If you are typing fast an trust autocomplete you most likely will be happy that the IDE is autocompleting w/o realizing there might be a trap.