代码改变世界

SQLte

2010-05-06 15:35  宝宝合凤凰  阅读(304)  评论(0)    收藏  举报

1)创建一个数据库(SQLite的一个文件的情况下)
 2)连接到数据库
3)创建一个表(一个数据库创建任何数字)
 4)插入到一个表更新数据搜索

  1 package
  2 {
  3     import flash.data.SQLConnection;
  4     import flash.data.SQLResult;
  5     import flash.data.SQLSchemaResult;
  6     import flash.data.SQLStatement;
  7     import flash.data.SQLTableSchema;
  8     import flash.events.Event;
  9     import flash.events.EventDispatcher;
 10     import flash.events.SQLErrorEvent;
 11     import flash.events.SQLEvent;
 12     import flash.filesystem.File;
 13 
 14     import mx.collections.ArrayCollection;
 15 
 16     public class DBUtil extends EventDispatcher
 17     {
 18         private var _myDB:File;
 19         private var _isOpen:Boolean = false;
 20         private var _dbConn:SQLConnection;
 21         private var _resultArrayCollection:ArrayCollection;
 22 
 23         public static const ASYNC_CONNECT_COMPLETE:String = "async_connect_complete";
 24         public static const ASYNC_SQL_COMPLETE:String = "async_sql_complete";
 25         public static const SCHEMA_COMPLETE:String = "schema_complete";
 26 
 27         public function get myDB():File
 28         {
 29             return _myDB;
 30         } 
 31 
 32         public function get isOpen():Boolean
 33         {
 34             return _isOpen;
 35         }
 36 
 37         public function get resultArrayCollection():ArrayCollection
 38         {
 39             return _resultArrayCollection;
 40         }
 41 
 42         public function DBUtil()
 43         {
 44             createLocalDB();
 45         }
 46 
 47         public function createLocalDB():void
 48         {
 49             var folder:File = File.applicationStorageDirectory.resolvePath('db');
 50             folder.createDirectory();
 51             _myDB = folder.resolvePath('myDBFile.db');
 52             openLocalDB(_myDB, true);
 53         }
 54 
 55         public function openLocalDB(dbFile:File, isAsync:Boolean):void
 56         {
 57             _dbConn = new SQLConnection();
 58 
 59             if(isAsync){
 60                 _dbConn.openAsync(dbFile);
 61 
 62                 _dbConn.addEventListener(SQLEvent.OPEN, sqlOpenHD);
 63                 _dbConn.addEventListener(SQLErrorEvent.ERROR, sqlOpenErrorHD);
 64 
 65             }else{
 66 
 67                 try{
 68                     _dbConn.open(dbFile);
 69                 }catch(e:SQLErrorEvent){
 70                     trace('SQL Error: ' + e.error.message);
 71                     trace('SQL Error Detail: ' + e.error.details);
 72                 }
 73             }
 74         }
 75 
 76         public function sqlOpenHD(e:SQLEvent):void
 77         {
 78             _isOpen = true;
 79             dispatchEvent(new Event(ASYNC_CONNECT_COMPLETE));
 80         }
 81 
 82         public function sqlOpenErrorHD(e:SQLErrorEvent):void
 83         {
 84             _dbConn.removeEventListener(SQLEvent.OPEN, sqlOpenHD);
 85             _dbConn.removeEventListener(SQLErrorEvent.ERROR, sqlOpenErrorHD);
 86             //trace('SQL Error: ' + e.error.message);
 87             //trace('SQL Error Detail: ' + e.error.details);
 88             dispatchEvent(e);
 89         }
 90 
 91         public function sqlExcuteErrorHD(e:SQLErrorEvent):void
 92         {
 93             var state:SQLStatement = e.target as SQLStatement;
 94             state.removeEventListener(SQLEvent.RESULT, sqlStatementResultHD);
 95             state.removeEventListener(SQLErrorEvent.ERROR, sqlExcuteErrorHD);
 96             //trace('SQL Error: ' + e.error.message);
 97             //trace('SQL Error Detail: ' + e.error.details);
 98             dispatchEvent(e);
 99         }
100 
101         public function executeSQLState(sqlText:String):void
102         {
103             if(isOpen == false){
104                 throw new Error('DB is not connected.');
105                 return;
106             }
107             var state:SQLStatement = new SQLStatement();
108             state.sqlConnection = _dbConn;
109             state.text = sqlText;
110             state.addEventListener(SQLEvent.RESULT, sqlStatementResultHD);
111             state.addEventListener(SQLErrorEvent.ERROR, sqlExcuteErrorHD);
112             state.execute();
113         }
114 
115         private function sqlStatementResultHD(e:SQLEvent):void
116         {
117             var state:SQLStatement = e.target as SQLStatement;
118             state.removeEventListener(SQLEvent.RESULT, sqlStatementResultHD);
119             state.removeEventListener(SQLErrorEvent.ERROR, sqlExcuteErrorHD);
120 
121             var result:SQLResult = state.getResult();
122             var temp:Array = result.data is Array ? result.data : [{rows:result.rowsAffected}];
123             _resultArrayCollection = new ArrayCollection(temp);
124             dispatchEvent(new Event(ASYNC_SQL_COMPLETE));
125         }
126 
127         public function getTableInfo():void
128         {
129             _dbConn.addEventListener(SQLEvent.SCHEMA, sqlSchemaCompHD);
130             _dbConn.addEventListener(SQLErrorEvent.ERROR, sqlSchemaErrorHD);
131             _dbConn.loadSchema();
132         }
133 
134         private function sqlSchemaCompHD(e:SQLEvent):void
135         {
136             _dbConn.removeEventListener(SQLEvent.SCHEMA, sqlSchemaCompHD);
137             _dbConn.removeEventListener(SQLErrorEvent.ERROR, sqlSchemaErrorHD);
138 
139             var result:SQLSchemaResult = _dbConn.getSchemaResult();
140             var tables:Array = result.tables;
141             var temp:Array = [];
142             for each(var tableSchema:SQLTableSchema in tables){
143                 temp.push({
144                     database:tableSchema.database,
145                     name:tableSchema.name,
146                     sql:tableSchema.sql,
147                     columns:tableSchema.columns.length
148                     });
149             }
150             _resultArrayCollection = new ArrayCollection(temp);
151             dispatchEvent(new Event(SCHEMA_COMPLETE));
152         }
153 
154         private function sqlSchemaErrorHD(e:SQLErrorEvent):void
155         {
156             _dbConn.removeEventListener(SQLEvent.SCHEMA, sqlSchemaCompHD);
157             _dbConn.removeEventListener(SQLErrorEvent.ERROR, sqlSchemaErrorHD);
158             //trace('SQL Error: ' + e.error.message);
159             //trace('SQL Error Detail: ' + e.error.details);
160             dispatchEvent(e);
161         }
162     }
163 }
164 

 

代码
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="620" height="420">
    
<mx:Script>
        
<![CDATA[
            import mx.controls.Alert;
            import mx.collections.ArrayCollection;

            
private var _dbUtil:DBUtil;

            [Bindable]
            
private var _myResultAC:ArrayCollection;

            
private function init():void
            {
                excuteBtn.addEventListener(MouseEvent.CLICK, excuteSQLBtnClickHD);
                tableInfoBtn.addEventListener(MouseEvent.CLICK, tableInfoBtnClickHD);

                _dbUtil 
= new DBUtil();
                _dbUtil.addEventListener(DBUtil.ASYNC_CONNECT_COMPLETE, dbAsyncCompHD);
            }

            
private function tableInfoBtnClickHD(e:MouseEvent):void
            {
                _dbUtil.addEventListener(DBUtil.SCHEMA_COMPLETE, dbSchemaCompHD);
                _dbUtil.addEventListener(SQLErrorEvent.ERROR, dbSchemaErrorHD);
                _dbUtil.getTableInfo();
            }

            
private function dbSchemaErrorHD(e:SQLErrorEvent):void
            {
                _dbUtil.removeEventListener(DBUtil.SCHEMA_COMPLETE, dbSchemaCompHD);
                _dbUtil.removeEventListener(SQLErrorEvent.ERROR, dbSchemaErrorHD);
                Alert.show(e.error.message 
+ '\n' + e.error.details);
            }

            
private function dbSchemaCompHD(e:Event):void
            {
                _dbUtil.removeEventListener(DBUtil.SCHEMA_COMPLETE, dbSchemaCompHD);
                _dbUtil.removeEventListener(SQLErrorEvent.ERROR, dbSchemaErrorHD);
                _myResultAC 
= _dbUtil.resultArrayCollection;
                
//Alert.show('table schema read complete');
            }

            
private function excuteSQLBtnClickHD(e:MouseEvent):void
            {
                _dbUtil.addEventListener(DBUtil.ASYNC_SQL_COMPLETE, dbSQLCompleteHD);
                _dbUtil.addEventListener(SQLErrorEvent.ERROR, dbSQLErrorHD);
                _dbUtil.executeSQLState(sqlInputTxt.text);
            }

            
private function dbSQLErrorHD(e:SQLErrorEvent):void
            {
                _dbUtil.removeEventListener(DBUtil.ASYNC_SQL_COMPLETE, dbSQLCompleteHD);
                _dbUtil.removeEventListener(SQLErrorEvent.ERROR, dbSQLErrorHD);
                Alert.show(e.error.message 
+ '\n' + e.error.details);
            }

            
private function dbSQLCompleteHD(e:Event):void
            {
                _dbUtil.removeEventListener(DBUtil.ASYNC_SQL_COMPLETE, dbSQLCompleteHD);
                _dbUtil.removeEventListener(SQLErrorEvent.ERROR, dbSQLErrorHD);
                _myResultAC 
= _dbUtil.resultArrayCollection;
                
//Alert.show('SQL excuted !');
            }

            
private function dbAsyncCompHD(e:Event):void
            {
                Alert.show(
"SQL connection complete !");
            }
        ]]
>
    
</mx:Script>
    
<mx:Panel x="0" y="0" width="588" height="388" layout="absolute" title="SQLTest" initialize="init();">
        
<mx:DataGrid x="10" y="111" width="548" height="227" id="myDG" dataProvider="{_myResultAC}"/>
        
<mx:Button x="10" y="81" label="SQL実行" id="excuteBtn"/>
        
<mx:Button x="88" y="81" label="table情報" id="tableInfoBtn"/>
        
<mx:TextArea x="10" y="10" width="548" height="63" id="sqlInputTxt" wordWrap="true" editable="true"/>
    
</mx:Panel>
</mx:WindowedApplication>

说明部分

1)创建一个数据库(SQLite的文件案件1)
2)连接到数据库会自动插入<br />部分,

public function createLocalDB():void
{
	var folder:File = File.applicationStorageDirectory.resolvePath('db');
	folder.createDirectory();
	_myDB = folder.resolvePath('myDBFile.db');
	openLocalDB(_myDB, true);
}

public function openLocalDB(dbFile:File, isAsync:Boolean):void
{
	_dbConn = new SQLConnection();

	if(isAsync){
		_dbConn.openAsync(dbFile);

		_dbConn.addEventListener(SQLEvent.OPEN, sqlOpenHD);
		_dbConn.addEventListener(SQLErrorEvent.ERROR, sqlOpenErrorHD);

	}else{

		try{
			_dbConn.open(dbFile);
		}catch(e:SQLErrorEvent){
			trace('SQL Error: ' + e.error.message);
			trace('SQL Error Detail: ' + e.error.details);
		}
	}
}

--------------------------------

随着1分贝为1(myDBFile.db)使文件,我觉得在同步或异步打开。
如果MXML中开始,国家已经这样做远。

 

1 CREATE TALBE test1(
2  id INTEGER PRIMARY KEY,
3  name TEXT,
4  phone TEXT
5 );
6 

 

 

 

 

1 INSERT INTO test1 VALUES(null'佐藤''0123-45-6789');
2 

 

 

 

 

1 SELECT * FROM test1;
2 

 

http://www.kuma-de.com/blog/2010-04-08/1755