Debbie Mp
Debbie Mp

Reputation: 163

fetch data from mysql and make a predicate in prolog

I have a table in mysql that includes 3 columns

table

id

name

age

I want to take the age value and make a rule in prolog that can find if this guy is child or adult or an elderly SO i want to have something like:

elderly(age):- age>60.

child(age):- age<12.

young(age):- age>12 and age<18.

adult(age):- age>18 and age<60.

Can anyone tell me how i can do that?

Upvotes: 0

Views: 995

Answers (1)

Daniel Lyons
Daniel Lyons

Reputation: 22803

First of all, variables in Prolog must be capitalized:

elderly(Age) :- Age > 60.
child(Age) :- Age < 12.
young(Age) :- Age > 12, Age < 18.
adult(Age) :- Age > 18, Age < 60.

Now connecting to the database will vary depending on your implementation. Odds are good you're using SWI, so the connection process is described in the documentation but will look something like this:

odbc_connect(mydb, [user(u), password(p), alias(mydb), open(once)]),
odbc_query(mydb, 'SELECT * FROM people', row(Id,Name,Age)),
elderly(Age), 
write('Found an elderly person: '), writeln(Name).

Hope this helps!

Upvotes: 1

Related Questions