问题
Sorry for my English. I want to create a simple website that will take data from the mysql database and display it on the page. I have two problems:
1) work with a database made in separate modules:
% database.pl
:- module(database,
[ create_db_connect/0,
use_database/0,
query_to_database/1,
disconnect_database/0
]).
:- use_module(library(odbc)).
create_db_connect :-
odbc_connect('test', _,
[ user('root'),
password('123')
alias(myblog),
open(once)
]).
use_database :-
odbc_query(myblog, 'use test', _).
query_to_database(X):-
odbc_query(myblog, 'SELECT data FROM testtable where id = 4', row(X)).
disconnect_database :- odbc_disconnect(myblog).
import this module in the main file:
% el.pl
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_parameters)).
:- use_module(library(http/html_head)).
:- use_module(database).
:- http_handler(root(.), home, []).
server(Port): -
http_server(http_dispatch, [port(Port)]).
home(_Request): -
reply_html_page (
title('Sql'),
[\ main_page
]).
main_page -->
create_db_connect,
use_database,
query_to_database(X),
disconnect_database,
html(div('id="tab_c2"', p('~w')-[X])).
In this case, get the error:
Warning: The predicates below are not defined. If these are defined
Warning: at runtime using assert / 1, use: - dynamic Name / Arity.
Warning:
Warning: create_db_connect/2, which is referenced by
Warning: /root/prologDev/el.pl:56:17: 1-st clause of main_page/2
But why? I defined it in the module database.pl!
2) Although I do not like the decision, but I adjusted the module database.pl on this:
:- module (database,
[ create_db_connect/2,
use_database/2,
query_to_database/3,
disconnect_database/2
]).
:- use_module(library(odbc)).
create_db_connect(_, _) :-
odbc_connect('test', _,
[ user('root'),
password('123'),
alias(myblog),
open(once)
]).
use_database(_, _) :-
odbc_query(myblog, 'use test', _).
query_to_database(X, _, _) :-
odbc_query(myblog, 'SELECT data FROM testtable where id = 4', row(X)).
disconnect_database(_, _) :- odbc_disconnect(myblog).
And now, the page is empty. When I stop swipl whit halt, an error occurs: Error in my_thread_global_end (): 1 threads did not exit.
what i`m doing wrong?
回答1:
Note that main_page//0
is a non-terminal, not a predicate. To call your database predicates from the main_page//0
, you need to write something like:
main_page -->
{create_db_connect,
use_database,
query_to_database(X),
disconnect_database},
html(div('id="tab_c2"', p('~w')-[X])).
The {}/1
construct allows you to call predicates from (the body of) grammar rules. Without it, create_db_connect
and the others would be interpreted as calls to other non-terminals (create_db_connect//0
, ...).
Typically, non-terminals are expanded into predicates by appending two arguments. Hence the warnings you got:
Warning: create_db_connect/2, which is referenced by
Warning: /root/prologDev/el.pl:56:17: 1-st clause of main_page/2
来源:https://stackoverflow.com/questions/26584861/swi-prolog-mysql-web