http://www.swi-prolog.org/pldoc/doc_for?object=section('packages/odbc.html')
This man page is not very easy to follow. It’s very technical how it’s written, describes the options but not in-context usage.
But you say you have the query down and want to make facts.
Given you want it all in memory anyway, I would suggest using the findall parameter, if it weren’t for this note:
The current implementation is incomplete. It does not allow arguments of row(...)
to be instantiated. Plain instantiation can always be avoided using a proper SELECT statement. Potentially useful however would be the translation of compound terms, especially to translate date/time/timestamp structures to a format for use by the application.
So instead, follow this pattern (not tested):
query_to_facts(Source, Query, Options, FactName) :-
odbc_query(Source, Query, Row, Options),
Row =.. [row | Terms],
Fact =.. [FactName | Terms],
assertz(Fact),
fail.
query_to_facts(Source, Query, FactName) :- true.
You could instead use findall/3 yourself:
query_to_facts(Source, Query, Options, FactName) :-
findall(Row, odbc_query(Source, Query, Row, Options), Rows),
maplist(post_fact(FactName), Rows).
And define post_fact/2 to do the =… and assertz as above.
post_fact(FactName, Row) :-
Row =.. [row | Terms],
Fact =.. [FactName | Terms],
assertz(Fact).
One more note: Use odbc_prepare over odbc_query if you have user-defined inputs to your query.