package demo; import static demo.sqlite.sqlite3_h.*; /** Return codes become exceptions. Each failure below is real SQLite output. */ public class Errors { static void attempt(String label, Runnable r) { try { r.run(); System.out.println(label + " -> no error"); } catch (SqliteException e) { System.out.println(label + " -> " + e.getMessage()); } } public static void main(String[] args) { attempt("open in a directory that does not exist", () -> Db.open("/no/such/dir/app.db").close()); attempt("open a missing file read-only", () -> Db.open("/tmp/jextract-demo-missing.db", SQLITE_OPEN_READONLY()).close()); try (Db db = Db.open(":memory:")) { attempt("syntax error", () -> db.exec("SELEC 1")); attempt("missing table", () -> db.exec("INSERT INTO ghost VALUES (1)")); db.exec("CREATE TABLE t(id INTEGER PRIMARY KEY, name TEXT NOT NULL)"); attempt("NOT NULL violation", () -> db.exec("INSERT INTO t(id) VALUES (1)")); db.exec("INSERT INTO t VALUES (1, 'a')"); attempt("PRIMARY KEY violation", () -> db.exec("INSERT INTO t VALUES (1, 'b')")); System.out.println("SQLITE_CONSTRAINT = " + SQLITE_CONSTRAINT() + ", sqlite3_errstr(19) = " + sqlite3_errstr(19).getString(0)); } } }