1 Reply Latest reply on Jan 3, 2011 3:03 AM by Starlover_jacob

    sqlite table exists

    Starlover_jacob Level 1

      Hi there,

       

      I would like to know how to check if a table exists.

      CREATE TABLE IF NOT EXISTS is not what i am looking for.

       

      I would like to create a simple function that returns true or false on a given table name.

      I did something like this;

           public function checktableOrCreate(tablename:String):void
           {
                        
                var sqlText1:String ="SELECT * FROM "+tablename+"";
                   createTableStatement = new SQLStatement();
                   createTableStatement.sqlConnection = conn;
                   createTableStatement.text = sqlText1;
                   createTableStatement.addEventListener(SQLErrorEvent.ERROR, errorHandler);
                   trace(createTableStatement.text);
                   try
                   {
                   createTableStatement.execute();
                   }
                   catch(error:SQLError)
                   {
                        trace("Error message:",error.message);
                        trace("Error message:",error.details);
                        if(error.errorID==3115)
                        {
                             trace("table doesn't exist so we create it");
                             
                             
                        }
                   }
           }
      

       

      The code runs into the catch which is nice.

      The only thing is that my application throws an error on the execute line and shows a box with the error.

      I know its supposed to do that, but i don't want that message box, i just want it to run into the catch and then call a function to create the table that doesn't exist.

       

      Please some help.

      Greets, J.

        • 1. Re: sqlite table exists
          Starlover_jacob Level 1

          Well, just minutes after posting this question i found the answer on the Internet, so here it is and i hope others can use it.

           

          This function returns true if table exists, and false if it doesn't

           

          private var createTableStatement:SQLStatement;
               public function checktableOrCreate(tablename:String):Boolean
               {
                         createTableStatement = new SQLStatement();
                         createTableStatement.sqlConnection = conn;
                         //statement.sqlConnection.open(null, SQLMode.READ);
                         // use SQLTableSchema to get tables only,
                         // see the loadSchema API Doc
                         createTableStatement.sqlConnection.loadSchema();
                         
                         var result:SQLSchemaResult = createTableStatement.sqlConnection.getSchemaResult();
                         var tableexists:Boolean=false;
                         for each (var table:SQLTableSchema in result.tables)
                         {
                              trace(table.name);
                              if(table.name==tablename)
                              {
                              tableexists=true;
                              break;
                              }
                         }         
                    return tableexists;
               }
          

           

           

          Greets, J.