Load schema and access the loaded schema of database in SQLite, AIR.

As always lets do things in steps.

  1. Create necessary variables.
  2. Open connection to database file already created.
  3. Read schema from opened connection to database.

1. Create necessary variables

private var sqlConnection:SQLConnection;
private var dbFile:File;
private var sqlStatement:SQLStatement;

2. Open connection to database file already created

dbFile=new File();
dbFile=File.applicationStorageDirectory.resolvePath("myDatabase.db");

sqlConnection = new SQLConnection();
sqlConnection.addEventListener(SQLEvent.OPEN, onDBOpened);
sqlConnection.addEventListener(SQLErrorEvent.ERROR, onDBError);
sqlConnection.open(dbFile);

3. Read schema from opened connection to database

//event handler for SQLEvent.OPEN

private function onDBOpened(event:SQLEvent):void
{
 if (event.type == "open")
 {
  //successfully opened database connection

  //below statement loads schema
  sqlConnection.loadSchema();
  //below statement to access the schema
  var sqlSchemaResult:SQLSchemaResult=sqlConnection.getSchemaResult();
  //below statement returns all tables from loaded schema result that is array of SQLTableSchema
  var tables:Array=sqlSchemaResult.tables;			
  for each(var sqlTableSchema:SQLTableSchema in tables)
  {
   trace("Table name:"+sqlTableSchema.name);
   trace("Columns:"+sqlTableSchema.columns);
   //can explore SQLTableSchema Class for more.
  }
 }
}

Hi People, Its been a while i blogged, trying to make it back and hope this post helps someone.

3 comments on “Load schema and access the loaded schema of database in SQLite, AIR.

  1. Pingback: Load schema and access the loaded schema of database in SQLite, AIR. | flex on blog - by Kumar | Everything about Flash | Scoop.it

  2. Pingback: Load schema and access the loaded schema of database in SQLite, AIR. | flex on blog – by Kumar « eaflash

  3. Nathan on said:

    Thank you. I have been trying to figure out how to use SQLSchema without much luck and your post was very helpful. Thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>