AIR and sqLite : if table exists conditional

余生长醉 提交于 2019-12-22 11:08:41

问题


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


回答1:


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;
}



回答2:


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...




回答3:


can be useful for someone - for async connection:

connection.loadSchema();
connection.addEventListener(SQLEvent.SCHEMA, check_result);

private function check_result(event:SQLEvent):void{
var schema:SQLSchemaResult = connection.getSchemaResult();
//as above
}


来源:https://stackoverflow.com/questions/4601707/air-and-sqlite-if-table-exists-conditional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!