<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="initApp()">

	<mx:Script>
		<![CDATA[

import mx.controls.Alert;

private var con:SQLConnection;
private var createStmt:SQLStatement;

private function initApp():void
{
	var file:File = File.applicationStorageDirectory.resolvePath("myTestdb.db")
	
	con = new SQLConnection();
	createStmt = new SQLStatement();
	
	try
	{
		con.open(file);
		
		 
		createStmt.sqlConnection = con; 
		var sql:String =  
    	"CREATE TABLE IF NOT EXISTS emp (" +  
    "    empId INTEGER PRIMARY KEY AUTOINCREMENT, " +  
    "    firstName TEXT, " +  
    "    lastName TEXT, " +  
    "    salary NUMERIC CHECK (salary > 0)" +  
    ")"; 
    
	createStmt.text = sql;
	createStmt.execute();  
	
		Alert.show("成功完成");
	}
	catch(error:SQLError)
	{
		Alert.show(error.message);
		Alert.show(error.details);
	}
}		
		
/* import mx.controls.Alert;

private var con:SQLConnection;
private var createStmt:SQLStatement;

private function initApp():void
{
	var file:File = File.applicationStorageDirectory.resolvePath("myTestdb.db")
	
	con = new SQLConnection();
	//在 openAsync() 方法调用操作成功完成时调度
	con.addEventListener(SQLEvent.OPEN,openHandler);
	//SQLConnection 对象的异步操作导致错误时调度
	con.addEventListener(SQLErrorEvent.ERROR,errorHandler);
	
	con.openAsync(file);
}

private function createResult(event:SQLEvent):void 
{ 
    Alert.show("表创建"); 
} 

private function createError(event:SQLErrorEvent):void 
{ 
    Alert.show("Error message:", event.error.message); 
    Alert.show("Details:", event.error.details); 
}

private function openHandler(evt:SQLEvent):void
{
	Alert.show("成功完成");
	
	createStmt = new SQLStatement(); 
	createStmt.sqlConnection = con; 
	var sql:String =  
    "CREATE TABLE IF NOT EXISTS emp (" +  
    "    empId INTEGER PRIMARY KEY AUTOINCREMENT, " +  
    "    firstName TEXT, " +  
    "    lastName TEXT, " +  
    "    salary NUMERIC CHECK (salary > 0)" +  
    ")"; 
    
	createStmt.text = sql;
	createStmt.addEventListener(SQLEvent.RESULT, createResult); 
	createStmt.addEventListener(SQLErrorEvent.ERROR, createError); 
	createStmt.execute();  
}

private function errorHandler(evt:SQLErrorEvent):void
{
	Alert.show("失败");
	Alert.show(evt.error.message);
	Alert.show(evt.error.details);
} */
		]]>
	</mx:Script>
</mx:WindowedApplication>
