swi序言mysql + web [英] swi prolog mysql + web
问题描述
对不起,我的英语. 我想创建一个简单的网站,该网站将从mysql数据库中获取数据并将其显示在页面上. 我有两个问题:
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)使用在单独的模块中建立的数据库:
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).
将此模块导入主文件:
% 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
但是为什么呢?我在模块database.pl中定义了它!
But why? I defined it in the module database.pl!
2)虽然我不喜欢这个决定,但是我对此进行了调整: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).
现在,该页面为空. 当我停止swipl whit停止时,会发生错误: my_thread_global_end()中的错误:1个线程没有退出.
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.
我做错了什么?
推荐答案
请注意,main_page//0
是非终结符,不是谓词.要从main_page//0
调用数据库谓词,您需要编写如下内容:
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])).
{}/1
构造允许您从(规则的)语法规则中调用谓词.没有它,create_db_connect
和其他将被解释为对其他非终端(create_db_connect//0
,...)的调用.
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
这篇关于swi序言mysql + web的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!