[TFS 1.2] function queryToTable(id, values)

Discussion in 'Mods & Lua Functions' started by Xeraphus, Dec 17, 2016.

  1. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,726
    Code:
    local dataTypes = {
        number = result.getNumber,
        string = result.getString,
        stream = result.getStream
    }
    
    function queryToTable(id, values)
        local ret = {}
        if not id then
            return ret
        end
        repeat
            local t = {}
            for i = 1, #values do
                local column, dataType = values[i]:match('(%a+):(%a+)')
                t[column] = dataTypes[dataType](id, column)
            end
            table.insert(ret, t)
        until not result.next(id)
        return ret
    end
    
    figured this code might be quite useful for when you want to iterate through queries
    created for this

    arguments:
    id = result id from query
    values = should be a table argument with strings for values you want to catch from each row

    example usage:
    Code:
    local resultId = db.storeQuery("SELECT * FROM `player_items` WHERE `player_id` = ".. guid)
    local results = queryToTable(resultId, {'pid:number', 'sid:number', 'itemtype:number', 'count:number'})
    for i = 1, #results do
        for k, v in pairs(results[i]) do
            print(k, v)
        end
    end
     
    Last edited: Dec 23, 2016
    HeberPcL, Joe Rod and president vankk like this.
  2. Colandus

    Colandus Well-Known Member

    Joined:
    Jun 6, 2007
    Messages:
    2,291
    Only getDataInt supported? Not very flexible
     
    Xeraphus and MatheusMkalo like this.
  3. Xeraphus

    Xeraphus the best

    Joined:
    Feb 14, 2015
    Messages:
    1,726
    updated
     
    Colandus likes this.

Share This Page