My First Yuma Sample
Posted on April 6, 2008
Filed Under REALbasic Tutorial
I figure that since Yuma is REALbasic under the hood (kind of ) that it should be fair game for this blog. =:)
This morning, instead of going to bed like I ought, I stayed up and wrote my first Yuma code. It’s very simple, just opens a SQLite database and reads in some data, then prints it to the web page. No classes, functions, etc, just some straight-ahead code.
Here’s the first chunk of Yuma code that goes at the top of the page:
<?yuma
dim srdata as SQLiteDatabase
dim rs as RecordSet
dim cnct as Boolean
dim articles() as String
srdata = New SQLiteDatabase
srdata.DatabaseFile = GetFolderItem("aa.rsd")
cnct = srdata.Connect
rs = srdata.SQLSelect("select title from articles")
while not rs.eof
articles.Append rs.Field("title").StringValue
rs.MoveNext
wend
srdata.Close
?>
One thing I want to mention right off the bat is this is NOT the way to write code — there’s no error- checking. But I wanted to make it as short as possible just to showcase the “good stuff.”
Just like regular RB code, you dimension your variables ahead of time, then create a FolderItem for the aa.rsd file, connect to the database, and finally get a RecordSet as the result of a SQL select statement.
Oh, the other thing I did there was instead of just printing out the data, I used Array.Append to fill the articles() array with the data — we’ll use it in the second chunk of code.
Here’s the second (and last) chunk of code — it’s the main part of the actual HTMP page:
<html>
<head><title>Yuma Database Sample</title></head>
<body>
<p>
<?yuma
dim x, numArticles as Integer
numArticles = UBound(articles)
for x = 0 to numArticles
print str(x+1) + ". " + articles(x) + "<br/>"
next
?>
<p/>
</body>
</html>
Right inside the body of the page we have another Yuma code block that simply finds out how big the articles() array is and then loops over it to print the data to the screen.
That block of code is in just about the simplest HTML page you can create, but there’s nothing stopping it from being inside a big honkin’ prettified web page.
Just for the fun of it, you could even do something like this — break the block up so that it loops over the rows of a table:
<table width="600">
<?yuma
dim x, numArticles as Integer
numArticles = UBound(articles)
for x = 0 to numArticles
?>
<tr>
<td>
<?yuma print str(x+1) + ". " + articles(x) + "<br/>" ?>
</td>
</tr>
<?yuma next ?>
</table>
Notice the “next” part of the for loop is in a different chunk of Yuma code — it doesn’t care, it still knows it belongs with the code above.
Put those two chunks of code on one .yuma page, make sure you have a database file called aa.rsd containing a table called articles and a column called title, and you’re in business!
If you haven’t download the development version of Yuma, get to it — it’s free and cross-platform.
I really wish I’d had Yuma last fall when I was porting a bunch of my REALbasic desktop apps to PHP. That silliness is going to stop now! =;)
Click here to get your copy of REALbasic
Comments
3 Responses to “My First Yuma Sample”
Leave a Reply
Thanks for the example, Jay! I’ve got a lot of PHP sitting on my drive that’s about to be ported…
As both an RB coder and a PHP coder, Yuma looks interesting. However, what I’m struggling to truly understand what Yuma’s selling point is.
PHP is free, has a rich in-built library and is fast enough for most applications. True, Yuma has static typing and a compiler (which are arguably good things), but what does it really bring to the table do you think? Is there going to be productivity gains? Scalability gains? Security gains?
Good question, Andrew — and I don’t know what the “official” word is on it. What I’m personally looking for is the ability to re-use my code.
I create desktop-based apps but I also have an online membership site which includes online versions of many of my tools. I had to rewrite all of those from scratch in PHP.
With Yuma my hope is that I’ll be able to take an existing app and create an online version in much less time than usual.
And the flip-side, too — if I use Yuma to create an online app and then decide a desktop version could increase sales, I can re-use that code as well.
Yes, the pluses of PHP are very strong, but as long as I’m an RB programmer it makes sense to me to look at using Yuma online rather than PHP.
Jay Jennings
(Full Disclosure: I’m still writing PHP code on my production server because I haven’t gotten around to buying the enterprise version of Yuma — all my Yuma pokings are happening on my local box at this time. I hope to change that soon.)