I have user input/ login as following:
login:- write('username: '), read(User), nl, write('password: '), read(Pass).
and i have a database with usernames and passwords. I use find predicate to get all rows as a list like shown in the code:
find(R):-findall(Row, rs('SELECT name FROM patient where name=\'James\'', Row), R).
?- find(R).
R = [row('James'), row('James'), row('James'), row('James')].
Is there a way to use user input from read() and check if that user is in database?
I tried member(User, R) but it’s not working properly.
I know prolog is not best language for this kind of stuff(login/registration). The reason why I’m doing this is that i’m working on helath care expert system in swi-prolog and i need login and reg for patients.
Is there a why this can be done? I’m new to prolog so im getting stuck on lots of stupid things.
Thanks!
………….
@gusbro I tried with member(row(User) as you told me:
check:-findall(Row, rs('SELECT name FROM patient where name=\'James\'', Row), R),
read(User), member(row(User), R);
write('wrong username!').
But i always get the same no matter what i write:
?- check.
|: Bla.
true ;
wrong username!
true.
?- check.
|: James.
true ;
wrong username!
true.
What you tried didn’t work because you are trying to unify terms of the form row(Atom) with Atom.
It should work fine if you do
member(row(User), R).However, note that in your example it will succeed more than once because you have repeated “rows”…
You might want to consider using
setof/3instead offindall/3, as it will eliminate duplicates from the resulting list (and as a side effect if will fail if there are no matches of the template of setof)