Post by turbov21 on Oct 11, 2007 0:29:18 GMT -5
A simple Guest Book application, to demonstrate how SQLite works in Run BASIC. Yes, Bobby Tables could probably crash this, but it's too late to get all the features in I wanted. Once 2.0 is working, I'll put this on the wiki.
[start]
cls
''' DISPLAY '''
' Starts the table
html "<table style=""width:100%;border:none;"">"
html "<tbody>"
html "<tr>"
html "<td style=""text-align:left;vertical-align:top;width:40%"">"
' The form
html "<p><b>Your Name:</b>"
textbox #yn, ""
html "</p>"
html "<p><b>Your Message:</b><br/>"
textarea #ym, ""
html "</p>"
link #add, "Add Message", [addnew]
print " ";
link #reset, "Reset", [start]
print ""
' Closes the first column
html "</td>"
html "<td style=""text-align:left;vertical-align:top;width:60%"">"
' Displays messages
html "<p>"
Call ListMessages
html "</p>"
' Closes the second column and table
html "</td></tr></tbody></table>"
wait
''' ADDS THE NEW MESSAGE '''
[addnew]
yn$ = #yn contents$()
ym$ = #ym contents$()
if yn$ = "" then yn$ = "Anonymous Coward" else yn$ = yn$
if ym$ = "" then ym$ = "Got nuthin to say." else ym$ = ym$
Call AddMsg yn$ ym$
goto [start]
''' FUNCTION AND SUB AREA '''
' Counts messages and gets a new, high, unique id number
function CountMsg$()
sqliteconnect #msg, "guestbook.db"
query$ = "select count(*) from guestbook"
#msg execute(query$)
result$ = #msg nextrow$("")
lastcount = val(result$) + 1
#msg disconnect()
CountMsg$ = str$(lastcount)
end function
' Lists all messages
sub ListMessages
sqliteconnect #msg, "guestbook.db"
query$ = "select * from guestbook order by id desc"
#msg execute(query$)
while #msg hasanswer()
result$ = #msg nextrow$(",")
Call BuildMsg result$
wend
#msg disconnect()
end sub
' Inserts the new message into the database
sub AddMsg nam$ msg$
lastcount$ = CountMsg$()
sqliteconnect #msg, "guestbook.db"
query$ = "insert into guestbook values ('" + nam$ + "', '" + msg$ + "', " + lastcount$ + ")"
#msg execute(query$)
#msg disconnect()
end sub
' Generates a single guestbook message
sub BuildMsg text$
name$ = ""
msg$ = ""
id$ = ""
commacnt = 0
for cnt = 1 to len(text$)
temp$ = mid$(text$, cnt, 1)
if temp$ = "," then
commacnt = commacnt + 1
else
select case commacnt
case 0
name$ = name$ + temp$
case 1
msg$ = msg$ + temp$
case 2
id$ = id$ + temp$
end select
end if
next cnt
html "<div id=""Message" + id$ + """>"
html "<p><b>" + name$ + "</b><br/>" + msg$ + "</p>"
html "</div>"
end sub