package AdaBase is type Error_Modes is (silent, warning, raise_exception); subtype SQL_State is String (1 .. 5); subtype Driver_Codes is Integer range -999 .. 4999; type Trax_ID is mod 2 ** 64; subtype Affected_Rows is Trax_ID; end AdaBase;
String function
AdaBase.Driver.Base.[DB].last_driver_message ()
This function returns a driver-specific error message regarding the last operation performed by the database handle.
Boolean function
AdaBase.Statement.Base.[STMT].last_driver_message ()
This function returns a driver-specific error message regarding the last operation performed by the statement handle.
with AdaBase;
with Connect;
with Ada.Text_IO;
procedure Fruit2 is
package CON renames Connect;
package TIO renames Ada.Text_IO;
numrows : AdaBase.Affected_Rows;
-- Intentionally broken UPDATE command (calories misspelled)
cmd : constant String := "UPDATE fruits set caloriesx = 14 " &
"WHERE fruit = 'strawberry'";
begin
CON.connect_database;
CON.DR.set_trait_error_mode (trait => AdaBase.raise_exception);
TIO.Put_Line ("SQL: " & cmd);
declare
begin
numrows := CON.DR.execute (sql => cmd);
TIO.Put_Line ("Result: Updated" & numrows'Img & " rows");
CON.DR.rollback;
exception
when others =>
TIO.Put_Line ("Error!");
TIO.Put_Line ("Driver message: " & CON.DR.last_driver_message);
TIO.Put_Line (" Driver code: " & CON.DR.last_driver_code'Img);
TIO.Put_Line (" SQL State: " & CON.DR.last_sql_state);
end;
CON.DR.disconnect;
end Fruit2;
Example code: testcases/fruit2/fruit2.adb
SQL: UPDATE fruits set caloriesx = 14 WHERE fruit = 'strawberry'
Error!
Driver message: Unknown column 'caloriesx' in 'field list'
Driver code: 1054
SQL State: 42S22
Output using the MySQL driver
SQL: UPDATE fruits set caloriesx = 14 WHERE fruit = 'strawberry'
Error!
Driver message: no such column: caloriesx
Driver code: 1
SQL State: HY000
Output using the SQLite driver
SQL: UPDATE fruits set caloriesx = 14 WHERE fruit = 'strawberry'
Error!
Driver message: ERROR: column "caloriesx" of relation "fruits" does not exist
LINE 1: UPDATE fruits set caloriesx = 14 WHERE fruit = 'strawberry'
^
Driver code: 2
SQL State: 42703
Output using the PostgreSQL driver
See stmt discard rest for another usage example.
[DB] is "MySQL.MySQL_Driver", "PostgreSQL.PostgreSQL_Driver", or "SQLite.SQLite_Driver"
[STMT] is "MySQL.MySQL_statement", "PostgreSQL.PostgreSQL_statement", or "SQLite.SQLite_statement"