AIR SQL 学习笔记(20080229)
29 二 2008
AIR 本地数据为SQLite,
applicationDirectory 路径为 %APPDATA%\appName\
applicationStorageDirectory路径为 %APPDATA%\appName\Local Store\
%APPDATA% 为系统环境变量,默认为 %SystemDrive%\Documents and Settings\%USERNAME%\Application Data
appName为AIR应用程序名称
组织出了一个基础的SQL类库。方便今后常用数据库操作。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | package {     import com.cbmland.data.SQL;     import flash.display.Sprite;     import flash.events.ErrorEvent;        import flash.events.Event;     import flash.filesystem.File;     import flash.text.TextField;     public class SQL_test extends Sprite      {         var sql:SQL= new SQL()         var _textField:TextField=new TextField()         public function SQL_test()         {             sql.addEventListener(SQL.RESULT,sql_RESULT)             sql.addEventListener(SQL.ERROR,sql_ERROR)             sql.open(File.applicationStorageDirectory.resolvePath("DBSample.db"))                     //尝试创建数据库以及一个表             var sqlText=                 "CREATE TABLE IF NOT EXISTS employees (" +                  "    empId INTEGER PRIMARY KEY AUTOINCREMENT, " +                  "    firstName TEXT, " +                  "    lastName TEXT, " +                  "    salary NUMERIC CHECK (salary > 0)" +                  ")";             query(sqlText)             //插入一个记录             sqlText="INSERT INTO employees (firstName, lastName, salary) " +                  "VALUES ('Bob', 'Smith', 8000)";             query(sqlText)             //查询             sqlText='SELECT * FROM employees '             query(sqlText)              _textField.multiline=true             _textField.width= stage.stageWidth             _textField.height= stage.stageHeight             this.addChild(_textField)         }         private function query(text:String){             sql.text=text             sql.execute()         }         private function view_RESULT(result):void                 {                     if(result.data!=null){                         var numRows:int = result.data.length;                         for (var i:int = 0; i < numRows; i++)                         {                             var output:String = "";                             for (var columnName:String in result.data[i])                             {                                 output += columnName + ": " + result.data[i][columnName] + "; ";                             }                             trace("row[" + i.toString() + "]\t", output);                             _textField.appendText("row[" + i.toString() + "] "+output+'\n')                         }                        // if(!result.complete){sql.next()}                     }else if(result.rowsAffected>0){                             trace(result.lastInsertRowID)                     }                 }          private function sql_RESULT(event:Event){             var result=(event.target.getResult())             view_RESULT(result)         }         private function sql_ERROR(event:ErrorEvent){             trace("Error message:", event['error'].message);             trace("Details:", event['error'].details);         }         } } | 
 AIR SQL 学习笔记(20080229) (9.8 KiB, 220 hits)
  AIR SQL 学习笔记(20080229) (9.8 KiB, 220 hits)

