Unable to open MemoDB record

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to open MemoDB record

mebeddo
Hi,

I'm new to Lua and Plua, and I am developing my first application for
my Palm Treo 650. I wanted a small program that reads memos in the
category "Shopping" and for each such memo sorts the lines
alphabetically. For example, if a memo in the "Shopping" category
contains the lines

    Laundry: Soap
    Dairy: Milk
    Dairy: Eggs
    Produce: Tomatoes
    Dairy: Butter
    Paper: Towels

then after running my application I'd like to see

    Dairy: Butter
    Dairy: Eggs
    Dairy: Milk
    Laundry: Soap
    Paper: Towels
    Produce: Tomatoes


Problem is, the application (source code pasted below) works fine in
the PalmOS simulator, but not on the Treo.

I successfully open the MemoDB, but inside the for loop the assert on
openrec(i) fails on the Treo. I am using Plua version 2. Below is the
source code (forgive the C flavor - I'm just learning Lua).

Can anyone help?

- Mike Beddo

-- shoppingsorter.lua
ui.title('Shopping Sorter')
f, n = io.open("db:/MemoDB", "r+b")
for i = 0, n-1, 1 do
        if f:getdbcat(f:getreccat(i)) == 'Shopping' then
                m = {}
                assert(f:openrec(i), "unable to open record")
               
                s = f:read("*l")
                while s do
                        table.insert(m, s)
                        s = f:read("*l")
                end
               
                table.sort(m)
                s = table.concat(m, "\n") .. "\n"
                print(s)
               
                assert(f:resizerec(i, string.len(s)+1), "unable to resize record")
                assert(f:write(s), "unable to update record")
                assert(f:closerec(), "unable to close record after update")
        end
end
f:close()
io.stderr:write('Done sorting Shopping memos!')






 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply | Threaded
Open this post in threaded view
|

Re: Unable to open MemoDB record

mebeddo
Some follow up after reading more messages from this group. The
following code works, but creates a new memo:

ui.title('Shopping Sorter')
f, n = io.open("db:/MemoDB", "r+b")
for i = 0, n-1, 1 do
        c = f:getreccat(i)
        if f:getdbcat(c) == 'Shopping' then
                f:openrec(i)
               
                m = {}
                s = f:read("*l")
                if s then
                        while s do
                                table.insert(m, s)
                                s = f:read("*l")
                        end
               
                        table.sort(m)
                        s = table.concat(m, "\n") .. "\n"
                end
               
                f:closerec()
               
                if s then
                        r = f:createrec(string.len(s)+1)
                        f:setreccat(r, c)
                        f:openrec(r)
                        f:write(s)
                        f:closerec()
                end

                --f:deleterec(i)
        end
end
f:close()
io.stderr:write('Done sorting Shopping memos!')

The following code, which attempts to edit the memo in place, works on
the PalmSim 5.4 but not on the Treo 650:

ui.title('Shopping Sorter')
f, n = io.open("db:/MemoDB", "r+b")
for i = 0, n-1, 1 do
        c = f:getreccat(i)
        if f:getdbcat(c) == 'Shopping' then
                f:openrec(i)
               
                m = {}
                for line in f:lines() do
                        table.insert(m, line)
                end
               
                if table.getn(m) > 0 then
                        table.sort(m)
                        sorted = table.concat(m, "\n")

                        f:seek("set")
                        f:write(sorted)
                end

                f:closerec()
        end
end
f:close()
ui.alert('Done sorting Shopping memos!')

In both scripts, what I've discovered is that f:openrec(i) always
returns nil on the Treo650 and the error string "Unknown error". If I
ignore the return value of nil I can read the records (I was expecting
nil for deleted records). I've also noticed on the Treo650 that
f:resizerec(i, size) doesn't work (I don't use it above because I
think the sorted memo will be the same size as the orginal memo).







 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/