[ library(mysql) | ]

sql_external(++Table, ++CacheMode)

Make a database relation available as a predicate
Table
Atom
CacheMode
Atom

Description

This predicate makes a data base relation available as a predicate, (similar to a set of facts). This is done by reading from the database the definition of relation Table, and generating a predicate whose name is the concatenation of 'r_' and the relation name Table. The arity and the order of arguments correspond to the definition of the relation's attributes.

CacheMode is the atom 'cache' or 'nocache'. It specifies whether the contents of the entity should be cached in main memory. Caching means that the whole relation gets retrieved from the database, every tuple converted to a fact, and the result compiled as a predicate. For large relations, this can take time and consume large amounts of memory.

Important note: For uncached predicates, every new invocation opens a new cursor into the relation resulting from the selection. This cursor only gets closed when either all tuples have been read, or when backtracking retrieval was terminated prematurely by a cut (!/0). In complex programs this can exceed the maximum number of cursors allowed by the interface. A possible solution is to a priori retrieve all tuples into a list (using findall/3), and then process the list using member/2.

Examples

    % Make the relation Adressen (which has attributes Id, Name, Vorname)
    % available as an (uncached) predicate
      ?- sql_external('Adressen',nocache).
      yes.

      ?- r_Adressen(I,N,V).
      I = 113
      N = 'Krause'
      V = 'Karl'     More? (;)

      I = 17
      N = 'Braesig'
      V = 'Berta'
      yes.
    

See Also