Populate List from Database

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
jesse
Posts: 205
Joined: Thu Nov 11, 2010 6:32 pm

Populate List from Database

Post by jesse » Tue Nov 16, 2010 4:46 pm

I am trying to determine how to populate a combobox with choices from a database. Below is the code I am working with that I originally used on a Datagrid. Can anyone advise?

Code: Select all

on mouseUp
  
  set traversalOn to true
   
   ## Connect to the database
    
    put "odbc" into theDBType
    put "SOLESERVER" into theDBHost
    put "[EVEREST_SOLESU].[dbo].[PERSONAL]" into tTableName    -- set this to the name of a table in your database
    put "example" into theDBUser
    put "example" into theDBPassword
    put revOpenDatabase( theDBType, theDBHost, theDBName, theDBUser, theDBPassword ) into theConnectionID

    if theConnectionID is an integer then
        ## Query the database for data
        put revQueryDatabase( theConnectionID, "SELECT PLAST FROM " & tTableName & "where active = 't'") into theCursor
        
        if theCursor is an integer then
            ConvertSQLCursorToArray theCursor, theDataGridArray
            put the result into theError
            
            if theError is empty then
                ## The cursor was successfully converted to an array.
                ## Assign it to the data grid. The 'firstname' and 'lastname' columns
                ## from the database cursor will appear in the matching columns
                ## in the data grid.
                set the dgData of group "DataGrid 1" to theDataGridArray
            end if
            
            ## Close the database cursor 
            revCloseCursor theCursor
        end if
        
        ## Close the database connection
        revCloseDatabase theConnectionID
    else
        answer "Error connecting to the database:" && theConnectionID & "."
     end if
     
     
end mouseUp

command ConvertSQLCursorToArray pCursor, @pOutArrayA
    local i
    local theFields
    local theError
    
    ## Get the names of all the columns in the database cursor
    put revDatabaseColumnNames(pCursor) into theFields
    if theFields begins with "revdberr," then
        put item 2 to -1 of theFields into theError
    end if
    
    if theError is empty then
        put 0 into i
        ## Loop through all rows in cursor
        repeat until revQueryIsAtEnd(pCursor)
            add 1 to i
            
            ## Move all fields in row into next dimension of the array
            repeat for each item theField in theFields
                put revDatabaseColumnNamed(pCursor, theField) into pOutArrayA[i][ theField ]
            end repeat
            
            revMoveToNextRecord pCursor
        end repeat
    end if
    
    return theError
end ConvertSQLCursorToArray
end mouseUp
Deving on WinVista Home Prem. SP2 32 bit. Using LiveCode 4.6.1 Pro Build 1392

MAatHome
Posts: 8
Joined: Sat May 12, 2007 4:00 am

Re: Populate List from Database

Post by MAatHome » Tue Nov 16, 2010 11:33 pm

Perhaps I can help with this one, I have a similar piece of code, into an option box, though I left out some of the error checking you have, its a fairly simple encapsulated example:

on updateEventList
local dbID
local tList,tSQL

put "SELECT Name FROM DrivingNews" into tSQL

put revOpenDatabase("MySQL",,"DrivingNews","xxx","xxx") into dbID
put revDataFromQuery(tab ,cr ,dbID ,tSQL) into tList
revCLoseDatabase(dbID)

if tList contains "revdberr" then return ""
set the rowDelimiter to cr
# the New... function below isn't implemented yet
put tList & cr & "-" & cr & "New..." into tList
set the text of button "Event" to tList
set the label of button "Event" to line 1 of the text of button "Event"
set menulines of button "Event" to the number of lines of tList
end updateEventList

Hope that helps,
Mike.

Post Reply