Database Records to Table Help

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

Database Records to Table Help

bh77a-2
Sunday, March 5, 2006

Good evening,

Been working on updating an old application of mine from Plua 1 to work under Plua
2.0b4 and have the following problem with the database...

- The database is created fine.  I can verify this using a file explorer program.
- I can add records with no problem, however the first record entered doesn't display in
the list UI object.  
- After entering the first record, all others display.

The issue is going to be with the way that I load the records into the table.  This occurs in
the DB Creation/Verification and Refresh sections.

Any thoughts on this would be greatly appreciated.  The code for the program is posted
below.

Thanks,
Brian

----------------START OF CODE------------------------------
-- P2_Randomizer.lua



-- Random Selection of Students App

-- Coded By: Brian Houghton

-- Initially Coded: 12/25/04 6:13 pm

-- Modified:  3/4/06 12:21 pm

-- Version: 1.11



--------------------------------------

-- Variables

--------------------------------------

Title="Randomizer"

Menu={"New Entry...","Choose","Delete Entry...","-","About Randomizer...","-","Quit"}

sf=1

nsbtnv="New Student..."

rdmbtnv="Choose"

dsbtnv="Delete Student"



--------------------------------------

-- Initialize

--------------------------------------

ui.destroy()

ui.clear()

w,h,d,hC=ui.mode()

ui.title(Title)

ui.menu(Menu)

if w==320 then sf=2 end



--------------------------------------

-- Draw Form

--------------------------------------

ui.moveto(10*sf,115*sf)

  nsbtnid=ui.button(nsbtnv)

ui.moveto(10*sf,130*sf)

  rdmbtnid=ui.button(rdmbtnv)

ui.moveto(10*sf,145*sf)

  dsbtnid=ui.button(dsbtnv)



--------------------------------------

-- DB Creation/Verification

--------------------------------------

f,n=io.open("db:/RandomizerDB","w")

f:close()

StudentTABLE={}

f,n=io.open("db:/RandomizerDB","r+")

for iN=0, n-1, 1 do

  f:openrec(iN)

  StudentTABLE[iN]=f:read("*a")

end

f:close()

ui.moveto(10*sf,20*sf)

StudentTABLEid=ui.list(8,25,StudentTABLE)

rdmnid=table.getn(StudentTABLE)



--------------------------------------

-- Refresh

--------------------------------------

function refresh ()

--------------------------------------

  ui.destroy()

  ui.clear()

  ui.title(Title)

  ui.menu(Menu)

  ui.moveto(10*sf,115*sf)

    nsbtnid=ui.button(nsbtnv)

  ui.moveto(10*sf,130*sf)

    rdmbtnid=ui.button(rdmbtnv)

  ui.moveto(10*sf,145*sf)

    dsbtnid=ui.button(dsbtnv)

  StudentTABLE={}

  f,n=io.open("db:/RandomizerDB","r+")

    for iN=0, n-1, 1 do

      f:openrec(iN)

      StudentTABLE[iN]=f:read("*a")

    end

  f:close()

  ui.moveto(10*sf,20*sf)

  StudentTABLEid=ui.list(8,25,StudentTABLE)

  rdmnid=table.getn(StudentTABLE)

end



--------------------------------------

-- choose

--------------------------------------

function choose()

--------------------------------------

  rdmnid2=rdmnid-1

  rdmret=math.random(0,rdmnid2)

    ui.setstate(StudentTABLEid,rdmret)

end



--------------------------------------

-- Delete Entry

--------------------------------------

function deleteentry()

--------------------------------------

  dltid=ui.getstate(StudentTABLEid)

  dltid2=dltid+1

  f=io.open("db:/RandomizerDB","r+")

    f:deleterec(dltid2)

  f:close()

  refresh ()

end



--------------------------------------

-- Empty

--------------------------------------

function empty()

--------------------------------------

-- nil

end



--------------------------------------

-- New Entry

--------------------------------------

function newentry()

--------------------------------------

  studentname=ui.input("Enter New Record...")

  if studentname~=nil then

    f,n=io.open("db:/RandomizerDB","r+")

    f:createrec(string.len(studentname))

    f:openrec(n)

      f:write(studentname)

    f:close()

    refresh ()

  elseif studentname==nil then

    empty()

  end

end



--------------------------------------

-- Event Loop

--------------------------------------

while 1 do

  ev,ctlid=ui.event()

  -- app stop

  if ev==ui.appStop then

    break

  end

  -- control handling

  if ev==ui.ctlSelect and ctlid==nsbtnid  then

    newentry()

  elseif ev==ui.ctlSelect and  ctlid==rdmbtnid then

    choose()

  elseif ev==ui.ctlSelect and ctlid==dsbtnid then

    deleteentry()

  end

  -- menu handling

  if ev==ui.menuSelect and ctlid==1

then

    newentry()

  elseif ev==ui.menuSelect and ctlid==2

then

    choose ()

  elseif ev==ui.menuSelect and ctlid==3

then

    deleteentry()

  elseif ev==ui.menuSelect and ctlid==5

then

        ui.alert([[Randomizer



                v 1.1

                b 017



                Programmed by:

                Brian Houghton]])

  elseif ev==ui.menuSelect and ctlid==7

then

    ui.clear()

    ui.destroy()

    break

  end

end






 
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: Database Records to Table Help

bh77a-2
Tuesday, March 7, 2006

Fixed it for anyone working with a similar issue...  Modified the code
for reading the database into the table to this:

for iN=0, n-1, 1 do
   f:openrec(iN)
   table.insert(StudentTABLE,f:read("*a"))
end

Also had to modify the Delete function so that it didn't subtract one
(1) from the selected table item.

Brian

On Mar 5, 2006, at 9:16 PM, bh77a wrote:

> Sunday, March 5, 2006
>
> Good evening,
>
> Been working on updating an old application of mine from Plua 1 to
> work under Plua
> 2.0b4 and have the following problem with the database...
>
> - The database is created fine.  I can verify this using a file
> explorer program.
> - I can add records with no problem, however the first record entered
> doesn't display in
> the list UI object.
> - After entering the first record, all others display.
>
> The issue is going to be with the way that I load the records into the
> table.  This occurs in
> the DB Creation/Verification and Refresh sections.
>
> Any thoughts on this would be greatly appreciated.  The code for the
> program is posted
> below.
>
> Thanks,
> Brian
>
> ----------------START OF CODE------------------------------
> -- P2_Randomizer.lua
>
>
>
> -- Random Selection of Students App
>
> -- Coded By: Brian Houghton
>
> -- Initially Coded: 12/25/04 6:13 pm
>
> -- Modified:  3/4/06 12:21 pm
>
> -- Version: 1.11
>
>
>
> --------------------------------------
>
> -- Variables
>
> --------------------------------------
>
> Title="Randomizer"
>
> Menu={"New Entry...","Choose","Delete Entry...","-","About
> Randomizer...","-","Quit"}
>
> sf=1
>
> nsbtnv="New Student..."
>
> rdmbtnv="Choose"
>
> dsbtnv="Delete Student"
>
>
>
> --------------------------------------
>
> -- Initialize
>
> --------------------------------------
>
> ui.destroy()
>
> ui.clear()
>
> w,h,d,hC=ui.mode()
>
> ui.title(Title)
>
> ui.menu(Menu)
>
> if w==320 then sf=2 end
>
>
>
> --------------------------------------
>
> -- Draw Form
>
> --------------------------------------
>
> ui.moveto(10*sf,115*sf)
>
>   nsbtnid=ui.button(nsbtnv)
>
> ui.moveto(10*sf,130*sf)
>
>   rdmbtnid=ui.button(rdmbtnv)
>
> ui.moveto(10*sf,145*sf)
>
>   dsbtnid=ui.button(dsbtnv)
>
>
>
> --------------------------------------
>
> -- DB Creation/Verification
>
> --------------------------------------
>
> f,n=io.open("db:/RandomizerDB","w")
>
> f:close()
>
> StudentTABLE={}
>
> f,n=io.open("db:/RandomizerDB","r+")
>
> for iN=0, n-1, 1 do
>
>   f:openrec(iN)
>
>   StudentTABLE[iN]=f:read("*a")
>
> end
>
> f:close()
>
> ui.moveto(10*sf,20*sf)
>
> StudentTABLEid=ui.list(8,25,StudentTABLE)
>
> rdmnid=table.getn(StudentTABLE)
>
>
>
> --------------------------------------
>
> -- Refresh
>
> --------------------------------------
>
> function refresh ()
>
> --------------------------------------
>
>   ui.destroy()
>
>   ui.clear()
>
>   ui.title(Title)
>
>   ui.menu(Menu)
>
>   ui.moveto(10*sf,115*sf)
>
>     nsbtnid=ui.button(nsbtnv)
>
>   ui.moveto(10*sf,130*sf)
>
>     rdmbtnid=ui.button(rdmbtnv)
>
>   ui.moveto(10*sf,145*sf)
>
>     dsbtnid=ui.button(dsbtnv)
>
>   StudentTABLE={}
>
>   f,n=io.open("db:/RandomizerDB","r+")
>
>     for iN=0, n-1, 1 do
>
>       f:openrec(iN)
>
>       StudentTABLE[iN]=f:read("*a")
>
>     end
>
>   f:close()
>
>   ui.moveto(10*sf,20*sf)
>
>   StudentTABLEid=ui.list(8,25,StudentTABLE)
>
>   rdmnid=table.getn(StudentTABLE)
>
> end
>
>
>
> --------------------------------------
>
> -- choose
>
> --------------------------------------
>
> function choose()
>
> --------------------------------------
>
>   rdmnid2=rdmnid-1
>
>   rdmret=math.random(0,rdmnid2)
>
>     ui.setstate(StudentTABLEid,rdmret)
>
> end
>
>
>
> --------------------------------------
>
> -- Delete Entry
>
> --------------------------------------
>
> function deleteentry()
>
> --------------------------------------
>
>   dltid=ui.getstate(StudentTABLEid)
>
>   dltid2=dltid+1
>
>   f=io.open("db:/RandomizerDB","r+")
>
>     f:deleterec(dltid2)
>
>   f:close()
>
>   refresh ()
>
> end
>
>
>
> --------------------------------------
>
> -- Empty
>
> --------------------------------------
>
> function empty()
>
> --------------------------------------
>
> -- nil
>
> end
>
>
>
> --------------------------------------
>
> -- New Entry
>
> --------------------------------------
>
> function newentry()
>
> --------------------------------------
>
>   studentname=ui.input("Enter New Record...")
>
>   if studentname~=nil then
>
>     f,n=io.open("db:/RandomizerDB","r+")
>
>     f:createrec(string.len(studentname))
>
>     f:openrec(n)
>
>       f:write(studentname)
>
>     f:close()
>
>     refresh ()
>
>   elseif studentname==nil then
>
>     empty()
>
>   end
>
> end
>
>
>
> --------------------------------------
>
> -- Event Loop
>
> --------------------------------------
>
> while 1 do
>
>   ev,ctlid=ui.event()
>
>   -- app stop
>
>   if ev==ui.appStop then
>
>     break
>
>   end
>
>   -- control handling
>
>   if ev==ui.ctlSelect and ctlid==nsbtnid  then
>
>     newentry()
>
>   elseif ev==ui.ctlSelect and  ctlid==rdmbtnid then
>
>     choose()
>
>   elseif ev==ui.ctlSelect and ctlid==dsbtnid then
>
>     deleteentry()
>
>   end
>
>   -- menu handling
>
>   if ev==ui.menuSelect and ctlid==1
>
> then
>
>     newentry()
>
>   elseif ev==ui.menuSelect and ctlid==2
>
> then
>
>     choose ()
>
>   elseif ev==ui.menuSelect and ctlid==3
>
> then
>
>     deleteentry()
>
>   elseif ev==ui.menuSelect and ctlid==5
>
> then
>
> ui.alert([[Randomizer
>
>
>
> v 1.1
>
> b 017
>
>
>
> Programmed by:
>
> Brian Houghton]])
>
>   elseif ev==ui.menuSelect and ctlid==7
>
> then
>
>     ui.clear()
>
>     ui.destroy()
>
>     break
>
>   end
>
> end
>
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>



 
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/