[TFS 1.2] function queryToTable(id, values)

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

  1. Xeraphus

    Xeraphus Support Team Support Team Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,263
    Likes Received:
    870
    Best Answers:
    90
    Code (Text):
    1.  
    2. local dataTypes = {
    3.     number = result.getNumber,
    4.     string = result.getString,
    5.     stream = result.getStream
    6. }
    7.  
    8. function queryToTable(id, values)
    9.     local ret = {}
    10.     if not id then
    11.         return ret
    12.     end
    13.     repeat
    14.         local t = {}
    15.         for i = 1, #values do
    16.             local column, dataType = values[i]:match('(%a+):(%a+)')
    17.             t[column] = dataTypes[dataType](id, column)
    18.         end
    19.         table.insert(ret, t)
    20.     until not result.next(id)
    21.     return ret
    22. end
    23.  
    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 (Text):
    1.  
    2. local resultId = db.storeQuery("SELECT * FROM `player_items` WHERE `player_id` = ".. guid)
    3. local results = queryToTable(resultId, {'pid:number', 'sid:number', 'itemtype:number', 'count:number'})
    4. for i = 1, #results do
    5.     for k, v in pairs(results[i]) do
    6.         print(k, v)
    7.     end
    8. end
     
    Last edited: Dec 23, 2016
    HeberPcL, Joe Rod and president vankk like this.
  2. Colandus

    Colandus Support Team Support Team

    Joined:
    Jun 6, 2007
    Messages:
    2,424
    Likes Received:
    156
    Best Answers:
    18
    Only getDataInt supported? Not very flexible
     
    Xeraphus and Mkalo like this.
  3. Xeraphus

    Xeraphus Support Team Support Team Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,263
    Likes Received:
    870
    Best Answers:
    90
    updated
     
    Colandus likes this.

Share This Page

Loading...