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

[showmyads]

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

Advertisements

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

  1. Nathan says:

    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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: