PDO: SQLite INSERT does not get saved
Checking the result of the errorInfo()
method after calling execute()
shows this error message:
NOT NULL constraint failed: Events.DBID
It's declared as DBID INT NOT NULL PRIMARY KEY
so why doesn't it just add the next sequential number automatically?
According to the SQLite documentation:
A PRIMARY KEY column only becomes an integer primary key if the declared type name is exactly "INTEGER".
So you need to change your column definition to DBID INTEGER PRIMARY KEY
.
Bonus Information:
Enable PDO exceptions so that these problems become fatal errors, and you don't need to manually call errorInfo()
:
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
This throws the following exception and halts execution:
Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: Events.DBID in ...
Bonus Information 2:
Since you are declaring foreign keys, you'll need to enable support for them at runtime as foreign key constraints are disabled by default (for backwards compatibility):
$db->exec('pragma foreign_keys = ON');