' CPDBdemo.ibas
{CREATORID "LDCD"}
{VERSION "1.0"}

' P$ is used to pass parameters to the 
' PP applet. Its structure is:
' 2 characters: function number
' n characters+chr$(10): 1st parameter
' and so on
' n characters: last parameter

CONST D$="CPDBdemoDB"

BEGIN
 A$=CHR$(10)
  
' Open CPDB
 PRINT "Hello"
 C$=CALLPP$(100,"01")

' Check if DB exists
 P$="03"+D$
 C$=CALLPP$(100,P$)

' If DB does not exist, create it
 IF C$="-1" THEN
  PRINT "Create CPDBdemoDB"
  P$="04"+D$+A$
  P$=P$+"LDCD"+A$
  P$=P$+"LEVEL=SHORTINT;SCORE=INT"
  C$=CALLPP$(100,P$)
 ENDIF

' Add one record
 L=RND(10)
 S=RND(100)
 PRINT "Adding one record"
 PRINT "  LEVEL = ";
 PRINT L USING 0
 PRINT "  SCORE = ";
 PRINT S USING 0 
 P$="05"+D$+A$
 P$=P$+"sI"+A$
 P$=P$+"LEVEL"+A$
 P$=P$+STR$(L,0)+A$
 P$=P$+"mI"+A$
 P$=P$+"SCORE"+A$
 P$=P$+STR$(S,0)
 C$=CALLPP$(100,P$) 

' Count number of records
 P$="06"+D$
 C$=CALLPP$(100,P$)
 A=VAL(C$)
 PRINT "Found ";
 PRINT C$;
 PRINT " records"

' Get sum of scores
 P$="07"+D$+A$
 P$=P$+"mI"+A$
 P$=P$+"SCORE" 
 C$=CALLPP$(100,P$)

' Calculate scores average
 A=VAL(C$)/A
 PRINT "Scores average = ";
 PRINT A USING 2 

' Close CPDB 
 PRINT "Bye"
 C$=CALLPP$(100,"02")
 SLEEP 5
END

