Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AIR and sqLite : if table exists conditional

How do I get a Boolean value in AS3 whether a table or an entry exists in the database?

like image 463
Daniel Avatar asked Nov 26 '25 06:11

Daniel


2 Answers

As opposed to finding it manually with SQL you should use the built in Schema information classes/functions. Here is an example of how it would work.

public function doesTableExist(connection:SQLConnection, tableName:String):Boolean
{
    connection.loadSchema();
    var schema:SQLSchemaResult = connection.getSchemaResult();

    for each (var table:SQLTableSchema in schema.tables)
    {
        if (table.name.toLowerCase() == tableName.toLowerCase())
        {
            return true;
        }
    }
    return false;
}
like image 166
Mindbane Avatar answered Nov 28 '25 15:11

Mindbane


There is no simple statement to achieve boolean value, but you can:

  1. use PRAGMA table_info(tbl_status) and analize list.

  2. try to execute SELECT col FROM table_name in try...catch block, in case of error simply set variable to bool.

BTW, maybe you need to use IF NOT EXISTS in create statement for table, index creation...

like image 35
Lex Avatar answered Nov 28 '25 15:11

Lex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!