The documentation on ODBC Parameterised queries suffers from a lack of clear examples (as does much of SWI Prolog, something I’m trying to help out with by writing tutorials).
The way I’ve done an insert into an SQL database is like so:
sql_escape_single_quotes(StringIn, StringOut) :-
split_string(StringIn, "'", "", List),
atomics_to_string(List, "''", StringOut).
db_insert(Title, Art) :-
sql_escape_single_quotes(Title, ETitle),
sql_escape_single_quotes(Art, EArt),
odbc_connect('blog', Connection, []),
odbc_query(Connection, "INSERT INTO arts (title, art) VALUES ('~w', '~w')"-[ETitle, EArt]),
odbc_disconnect(Connection).
I’ve edited the code below from a previous post after taking the time to get it to work. In my original post, I put [Title, Art] as the parameters in odbc_prepare instead of odbc_execute, and simply replaced them with [default, default] in odbc_prepare since I find the alternatives in the provided documentation incomprehensible:
db_insert(Title, Art) :-
odbc_connect('blog', Connection, []),
odbc_prepare(Connection, "INSERT INTO arts (title, art) VALUES (?, ?)", [default, default], Statement),
odbc_execute(Statement, [Title, Art]),
odbc_disconnect(Connection).
Thing’s I’m still in the dark on is if the findall option used by odbc_query works.
I’ve generally found ODBC to be an awful, Microsoft, technology and spent several hours yesterday figuring out why my SWI Prolog web application would not work on a new server running Postgresql 11. (The answer was in the pg_hba.conf file where the default ident refused to work with ODBC and had to be changed to trust)