1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
# quotations - create a database of quotations
# new and improved to use subroutines and functions
# 2012-11-25 j.m.reneau
# create a new database file or open it or create it with a few good quotes
dbfile$ = "quotations.sqlite3"
e = exists(dbfile$) # set e to true if database already exists
# open the database - open databases are global to program
# dboopen without a database number opens the database as 0
dbopen dbfile$
# if the database did not exist then create the tables and populate
# with initial set og quotaions
if (not e) then call createquotetable()
print "Database has " + quotecount() + " quotes."
# display a menu until the user chooses a zero
do
print
print "1 - view a random quote "
print "2 - list all quotes"
print "3 - add a quote"
print "0 - quit"
input "0,1,2,or 3 >", choice
if choice = 1 then call showrandomquote()
if choice = 2 then call showallquotes()
if choice = 3 then call addnewquote()
until choice = 0
# wrap everything up
print "Good bye."
dbclose
end
subroutine showrandomquote()
# generate a random integer from 1 to the number of quotes
# and display that quotation
quote = 1 + int(rand * quotecount())
call showquote(quote)
end subroutine
subroutine showquote(n)
# given a quote number n - display a single quotation
stmt$ = "select author, saying from quotes where ROWID = " + n + ";"
dbopenset stmt$
if dbrow() then
print dbstring("saying")
print " - " + dbstring("author")
else
print "Quotation " + n + " is not on file."
end if
dbcloseset
end subroutine
subroutine showallquotes()
# show all quotes in the table
dbopenset "select rowid, author, saying from quotes order by rowid;"
while dbrow()
print dbint("rowid") + " " + dbstring("saying") + " (" + dbstring("author") + ")"
end while
dbcloseset
end subroutine
subroutine addnewquote()
# ask the user to enter in a new quote
# do not allow
input "Quote>", quote$
input "Author>", author$
if quote$ = "" or author$ = "" then
print "You must supply both a quote and an author to add."
else
id = insertquote(author$, quote$)
print "Quote " + id + " added."
end if
end subroutine
function quotecount()
# return the quotation count
# get an unused set number for database 0 for this operation
quotecount = 0
tempset = freedbset(0)
dbopenset 0, tempset, "select count(*) as k from quotes"
if dbrow(0, tempset) then
quotecount = dbint(0, tempset, "k")
end if
dbcloseset 0,tempset
end function
subroutine createquotetable()
# create the quotes table a populate an initial group of quotations into it
# sqlite automatically creates a primary key column called ROWID and assigns a unique value
dbexecute "create table quotes (author text, saying text);"
id = insertquote('Abraham Lincoln (1809 - 1865)','Most folks are about as happy as they make up their minds to be.')
id = insertquote('George Burns (1896 - 1996)','Happiness is having a large, loving, caring, close-knit family in another city.')
id = insertquote('Jean Houston','At the height of laughter, the universe is flung into a kaleidoscope of new possibilities.')
id = insertquote('Doug Larson', 'Home computers are being called upon to perform many new functions, including the consumption of homework formerly eaten by the dog.')
id = insertquote( 'Isaac Asimov', 'I do not fear computers. I fear the lack of them.')
id = insertquote( 'Pierre Gallois', 'If you put tomfoolery into a computer, nothing comes out of it but tomfoolery. But this tomfoolery, having passed through a very expensive machine, is somehow ennobled and no-one dares criticize it.')
id = insertquote('Robert Orben', 'To err is human--and to blame it on a computer is even more so.')
id = insertquote('Herm Albright (1876 - 1944)', 'A positive attitude may not solve all your problems, but it will annoy enough people to make it worth the effort.')
id = insertquote('William James (1842 - 1910)', 'The greatest discovery of my generation is that a human being can alter his life by altering his attitudes of mind.')
end subroutine
function insertquote(author$, saying$)
# insert a single quotation into the database table
dbexecute "insert into quotes (author, saying) values (" + q$(author$) +", " + q$(saying$) + ");"
# now get the primary key (ROWID) that was assigned to the entry
dbopenset "select last_insert_rowid()"
if dbrow() then
insertquote = dbint(0)
else
insertquote = -1
end if
dbcloseset
end function
function q$(a$)
# put single quotes around a string after changing single quotes to two single quotes
q$ = "'" + replace(a$,"'","''") + "'"
end function
|