【发布时间】:2014-12-22 11:19:59
【问题描述】:
对不起我的英语。 我想创建一个简单的网站,该网站将从 mysql 数据库中获取数据并将其显示在页面上。 我有两个问题:
1) 使用在不同模块中创建的数据库:
% 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])).
在这种情况下,得到错误:
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中定义的!
2)虽然我不喜欢这个决定,但是我在这个模块上调整了database.pl:
:- 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 halt 时,会发生错误: my_thread_global_end () 中的错误:1 个线程没有退出。
我做错了什么?
【问题讨论】:
标签: mysql web swi-prolog