Does TABLE exist?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • azmanar
    Indigo Rose Customer
    • Oct 2004
    • 1020

    Does TABLE exist?

    Hi,

    Does anyone know how to perform a check whether a TABLE exist or not?

    The main purpose is to check whether a TABLE has been created or not. If TABLE has been created then do something. If the TABLE does not exist, do something else.

    Thank you.
    Newbie Examples
    ------> AMS 7.5 : amstudio.azman.info
    ----> AMS 6 & 5: www.azman.info/ams/
    ----> FB: facebook.com/GuideToWealth

    ----> Content Development Blog: www.AZMAN.asia
  • ShadowUK
    No longer a forum member
    • Oct 2007
    • 1321

    #2
    Originally posted by azmanar View Post
    Hi,

    Does anyone know how to perform a check whether a TABLE exist or not?

    The main purpose is to check whether a TABLE has been created or not. If TABLE has been created then do something. If the TABLE does not exist, do something else.

    Thank you.
    Code:
    if (type(Value) == "table") then
        -- It's a table.
    else 
        -- It's not a table.
    end
    I hope you don't mean SQLite

    Comment

    • RizlaUK
      Indigo Rose Customer
      • May 2006
      • 5478

      #3
      i usally just try and create the table, if already exists then sqlite throws an error "(30001) table already exists"

      eg
      Code:
      if not db then
      	db =  SQLite.Open(_TempFolder .. "\\MyDatabase.db");
      end
      
      -- create a table with 3 columns
      SQLite.Query(db,"create table foo(a integer primary key, b text, c integer)");
      
      err = Application.GetLastError();
      if err == SQLite.ERROR then
      	if err == 30001 then
      		-- table exist
      	    strErrMsg = SQLite.GetLastErrorString();
      	    Dialog.Message("Notice", "table already exist");
      	else
      		-- generic error
      	    strErrMsg = SQLite.GetLastErrorString();
      	    Dialog.Message("Error:"..err, strErrMsg, MB_OK, MB_ICONEXCLAMATION);
      	end
      else
      	-- no table or error
      	
      end
      if (type(Value) == "table") then
      -- It's a table.
      else
      -- It's not a table.
      end
      if only it were that easy, lol
      Embrace change in your life, you never know, it could all work out for the best

      Comment

      • azmanar
        Indigo Rose Customer
        • Oct 2004
        • 1020

        #4
        Hi,

        Sorry. What I meant was LUA Table and not SQLite Table.

        So, anyone has ideas about this?

        I need to search whether a Table already exist. If it does not exist, I will do something.
        Newbie Examples
        ------> AMS 7.5 : amstudio.azman.info
        ----> AMS 6 & 5: www.azman.info/ams/
        ----> FB: facebook.com/GuideToWealth

        ----> Content Development Blog: www.AZMAN.asia

        Comment

        • ShadowUK
          No longer a forum member
          • Oct 2007
          • 1321

          #5
          Originally posted by azmanar View Post
          Hi,

          Sorry. What I meant was LUA Table and not SQLite Table.

          So, anyone has ideas about this?

          I need to search whether a Table already exist. If it does not exist, I will do something.
          Well, I did explain in my first post. But if you mean find if a table exists from a string?

          Comment

          • RizlaUK
            Indigo Rose Customer
            • May 2006
            • 5478

            #6
            in that case shadow had it :yes

            a more generic approch
            Code:
            function TableDoesExist(t)
            	if t and (type(t) == "table") then
            		return true
            	else
            		return false
            	end 
            end
            
            -- useage
            if TableDoesExist(t) then
            	-- It's a table.
            else
            	-- It's not a table.
            end
            Embrace change in your life, you never know, it could all work out for the best

            Comment

            • azmanar
              Indigo Rose Customer
              • Oct 2004
              • 1020

              #7
              Hi,

              Thanks Shadow & Rizla.

              The function is exactly what I needed.

              If anyone has other ideas, please share with us here.
              Newbie Examples
              ------> AMS 7.5 : amstudio.azman.info
              ----> AMS 6 & 5: www.azman.info/ams/
              ----> FB: facebook.com/GuideToWealth

              ----> Content Development Blog: www.AZMAN.asia

              Comment

              • reteset
                Indigo Rose Customer
                • May 2006
                • 1688

                #8
                if a table exist its a table even if it is empty,
                if specified unknown variable is not a table it is nil
                or any other type (number , string ,boolean)
                amsplugins.com Is Closed.

                Facebook Page

                Comment

                • azmanar
                  Indigo Rose Customer
                  • Oct 2004
                  • 1020

                  #9
                  AMS Value Types

                  Hi,

                  Exactly, Reteset. Including function.

                  Shadow's function is useful to identify value types and check if it existed. There are 6 value types in AMS, i.e, Number, String, Boolean, Table, Function and Nil.

                  Thank you Shadow, Rizla and Reteset. Very good tips.
                  Newbie Examples
                  ------> AMS 7.5 : amstudio.azman.info
                  ----> AMS 6 & 5: www.azman.info/ams/
                  ----> FB: facebook.com/GuideToWealth

                  ----> Content Development Blog: www.AZMAN.asia

                  Comment

                  • ShadowUK
                    No longer a forum member
                    • Oct 2007
                    • 1321

                    #10
                    Originally posted by azmanar View Post
                    Hi,

                    Exactly, Reteset. Including function.

                    Shadow's function is useful to identify value types and check if it existed. There are 6 value types in AMS, i.e, Number, String, Boolean, Table, Function and Nil.

                    Thank you Shadow, Rizla and Reteset. Very good tips.
                    You forgot userdata and thread.

                    Comment

                    Working...
                    X