This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

SQL challenge yet again
#2
Hi
I'm not sure what is your end game here but you can take an idea from this script how to grab range of objects from DB and do some action on them.
This script will create tags on object based on the range and the separators selected and the object name.

Code:
seperators = '.,_ =+'

-- Start address of objects (min: 0/0/1 = main/sub/address)
start_main = 0
start_sub = 0
start_address = 1

-- End address of objects (max: 31/7/255 = main/sub/address)
end_main = 15
end_sub = 7
end_address = 255

------------------------------------------ End Parameters ------------------------------------------
------------------------------ DON'T CHANGE ANYTHING UNDER THIS LINE -------------------------------

function split(source, delimiters)
        local elements = {}
        local pattern = '([^'..delimiters..']+)'
        string.gsub(source, pattern, function(value) elements[#elements + 1] =     value;  end);
        return elements
end

-- Calculate start address to DB format
start_objectaddress = ((start_main * 2048) + (start_sub * 256) + start_address)

-- Calculate end address to DB format
end_objectaddress = ((end_main * 2048) + (end_sub * 256) + end_address)

-- Get all objects from DB
all_objects = db:getall('SELECT address, name, tagcache FROM objects')
for _, object in ipairs(all_objects) do
  -- Check if object is inside given range
  if object.address >= start_objectaddress and object.address <= end_objectaddress then
     currentrowaddress = object.address
    myobject = grp.find(currentrowaddress)
    nameTable = split(myobject.name, seperators)  --spliting  object string in to table
   
  --  log(nameTable)
    for _, tag in ipairs(nameTable) do
      tagvalue = tag
   
       -- Check if object tag already exists inside DB
     current_object_tag = db:getall('SELECT object, tag FROM objecttags WHERE object = ' .. currentrowaddress .. ' AND tag = "' .. tagvalue .. '"')
     object_tagcache = db:getone('SELECT tagcache FROM objects WHERE address = ' .. currentrowaddress .. '')
     if #current_object_tag == 0 then
        -- Add object to table objecttags
        db:insert('objecttags', {object = currentrowaddress, tag = tagvalue, })
          -- Check if object already has other tag values inside DB and add new values to tagcache
        if object_tagcache == nil or object_tagcache == "" then
          db:update('objects', { tagcache = tagvalue }, { address = currentrowaddress })
            else
          tag_string = "" .. object_tagcache .. ", " .. tagvalue .. ""
               log(tag_string)
          db:update('objects', { tagcache = tag_string }, { address = currentrowaddress })
          end
     end
   
    end
  end
end

script.disable(_SCRIPTNAME)
------------------------------
Ctrl+F5
Reply


Messages In This Thread
SQL challenge yet again - by Trond Hoyem - 16.09.2019, 13:31
RE: SQL challenge yet again - by Daniel - 16.09.2019, 14:25
RE: SQL challenge yet again - by admin - 16.09.2019, 18:50
RE: SQL challenge yet again - by Trond Hoyem - 17.09.2019, 19:43
RE: SQL challenge yet again - by Trond Hoyem - 20.09.2019, 12:00
RE: SQL challenge yet again - by Daniel - 20.09.2019, 12:02
RE: SQL challenge yet again - by Trond Hoyem - 20.09.2019, 12:09
RE: SQL challenge yet again - by Trond Hoyem - 25.09.2019, 06:33

Forum Jump: