[TFS 1.2] function queryToTable(id, values)

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

  1. Static_

    Static_ Premium User Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,807
    Likes Received:
    1,155
    Best Answers:
    141
    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 Well-Known Member

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

    Static_ Premium User Premium User

    Joined:
    Feb 14, 2015
    Messages:
    2,807
    Likes Received:
    1,155
    Best Answers:
    141
    updated
     
    Colandus likes this.

Share This Page

Loading...