Example of AS3 self-reading mysql

Source: Internet
Author: User

Reprinted from: http://hi.baidu.com/silvanote/blog/item/a5aab77996d458f90bd18748.html

 

To achieve this, the assql class library can perform some basic data operations even though the current version is not stable.

Organize some of its instances and create an AIR instance! The list function is added to query now!



Step 1: Create a MYSQL database:

Assql-test

Step 2: execute the following SQL statement:

Create table if not exists 'use _ db '(
'Id' float not null AUTO_INCREMENT,
'Names' char (25) COLLATE utf8_bin not null,
'Pass' char (25) COLLATE utf8_bin not null,
Primary key ('id ')
) ENGINE = MyISAM default charset = utf8 COLLATE = utf8_bin AUTO_INCREMENT = 10;

Step 3: Create an FB4 AIR project named AsSql

All the mxml code is copied and pasted.

<? Xml version = "1.0" encoding = "UTF-8"?> <S: Drawing wedapplication xmlns: fx = "http://ns.adobe.com/mxml/2009" xmlns: s = "library: // ns.adobe.com/flex/spark" creationComplete = "init ()" xmlns: mx = "library: // ns.adobe.com/flex/mx "width =" 600 "height =" 300 "> <fx: Script> <! [CDATA [import com. maclema. mysql. connection; import com. maclema. mysql. mySqlToken; import com. maclema. mysql. resultSet; import com. maclema. mysql. statement; import com. maclema. util. resultsUtil; import mx. controls. alert; import mx. rpc. asyncResponder; private var con: Connection; private function init (): void {onCreationComplete () ;} private function onCreationComplete (): void {con = new Connection ("localhost", 3306, "root", "", "assql-test"); con. addEventListener (Event. CONNECT, handleConnected); con. connect ();} private function handleConnected (e: Event): void {getList ();} private function getTab (_ id: int): void {var st: Statement = con. createStatement (); st. SQL = "SELECT * FROM use_db WHERE id =" + String (_ id); var token: MySqlToken = st.exe cuteQuery (); token. addResponder (new AsyncResponder (function (data: Object, token: Object): void {var rs: ResultSet = ResultSet (data); while (rs. next () {out_txt.text = rs. getString ("names") + ":" + rs. getString ("pass") ;}, function (info: Object, token: Object): void {Alert. show ("Error:" + info) ;}, token);} private function getList (_ s: int = 0, _ e: int = 100 ): void {var st: Statement = con. createStatement (); st. SQL = "SELECT * FROM 'use _ db' LIMIT 0, 30"; var token: MySqlToken = st.exe cuteQuery (); token. addResponder (new AsyncResponder (function (data: Object, token: Object): void {var rs: ResultSet = ResultSet (data); var obj: Array = new Array (); while (rs. next () {Debug. log (rs. getString ("names"); obj. push ({ids: rs. getNumber ("id"), names: rs. getString ("names"), pass: rs. getString ("pass")});} dataList. dataProvider = obj;}, function (info: Object, token: Object): void {Alert. show ("Error:" + info) ;}, token) ;}private function wirteData (user: String, pass: String ): void {// insert into 'assql-test '. 'Use _ db' ('id', 'names', 'pass') VALUES (NULL, '',''), (NULL, 'qq ', 'wwe123'); var st: Statement = con. createStatement (); st. SQL = "INSERT INTO 'assql-test '. 'Use _ db' ('id', 'names', 'pass') VALUES (NULL, '"+ user +"', '"+ pass + "')"; var token: MySqlToken = st.exe cuteQuery (); token. addResponder (new AsyncResponder (function (data: Object, token: Object): void {if (data. affectedRows> = 1) {Alert. show ('written successfully' + String (data. affectedRows) + 'data'); getList () ;}}, function (info: Object, token: Object): void {Alert. show ("Error:" + info) ;}, token) ;}]]> </fx: Script> <s: HGroup> <s: VGroup> <s: label text = "Operation Database:"/> <mx: Form> <mx: FormItem label = "id:"> <s: textInput id = "input_txt"/> </mx: FormItem> <s: Button label = "read" click = "getTab (int (input_txt.text )) "/> </mx: FormItem> <mx: FormItem label =" output: "> <s: textArea id = "out_txt" height = "50" width = "100%"/> </mx: FormItem> <mx: HRule width = "100%"/> <mx: formItem label = "names:"> <s: TextInput id = "input0000txt"/> </mx: FormItem> <mx: FormItem label = "pass:"> <s: textInput id = "input2_txt"/> </mx: FormItem> <s: Button label = "write" click = "wirteData (input1_txt.text, input2_txt.text) "/> </mx: FormItem> </mx: Form> </s: VGroup> <mx: VRule id =" line "height =" 100% "/> <s: VGroup> <s: Label text = "database list:"/> <mx: DataGrid rowCount = "3" id = "dataList" height = "200"> <mx: columns> <mx: DataGridColumn dataField = "ids" dataTipField = "id"/> <mx: DataGridColumn dataField = "names"/> <mx: dataGridColumn dataField = "pass"/> </mx: columns> </mx: DataGrid> </s: VGroup> </s: HGroup> </s: WindowedApplication>

Note:

Con = new Connection ("localhost", 3306, "root", "", "assql-test"); is the configuration correct?

Parameter 1 is your local IP address or Internet IP Address

Parameter 2 Port

Parameter 3 mysql user name

Parameter 4 mysql password

Parameter 5 Name of the database to be connected

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.