import flash.data.SQLResult;
import flash.filesystem.File;
import flash.data.SQLStatement;
import flash.data.SQLConnection;
import flash.events.SQLEvent;
import flash.events.SQLErrorEvent;
import flash.errors.SQLError;
import fl.data.DataProvider;
var dbFile:File = new File("app-resource:/test01.db");
var dbConn:SQLConnection = new SQLConnection();
var dbStatement:SQLStatement;
var insertCount:uint = 0;
dbConn.addEventListener(SQLEvent.OPEN, onDBOpened);
dbConn.addEventListener(SQLErrorEvent.ERROR, onDBError);
// 若檔案不存在, 會自動建立
dbConn.open(dbFile);
function onDBOpened(e:SQLEvent) {
trace("onDBOpened:", e);
if (e.type == SQLEvent.OPEN) {
dbStatement = new SQLStatement();
dbStatement.sqlConnection = dbConn;
dbStatement.text = "select * from Users";
dbStatement.addEventListener(SQLEvent.RESULT, onSQLResult);
dbStatement.addEventListener(SQLErrorEvent.ERROR, onSQLError);
dbStatement.execute();
}
}
function onDBError(e:SQLErrorEvent) {
trace("onDBError:", e);
}
function onSQLResult(e:SQLEvent) {
trace("onSQLResult:", e);
var d:Array = dbStatement.getResult().data;
QopDump.echo(d);
dataGrid.dataProvider = new DataProvider(d);
dbConn.close();
}
function onSQLError(e:SQLErrorEvent) {
trace("onSQLError:", e);
var err:SQLError = e.error;
trace("Error Code:", err.code);
trace("Operation:", err.operation);
trace("Error Message:", err.message, "\n");
if (err.message.indexOf("no such table") != -1) {
dbStatement = new SQLStatement();
dbStatement.sqlConnection = dbConn;
dbStatement.text = "CREATE TABLE Users "+
"(id INTEGER PRIMARY KEY, username TEXT, password TEXT);";
dbStatement.addEventListener(SQLEvent.RESULT, doInsert);
dbStatement.addEventListener(SQLErrorEvent.ERROR, onSQLError);
dbStatement.execute();
}
}
function doInsert(e:SQLEvent) {
insertCount++;
if(insertCount>3) return;
dbStatement = new SQLStatement();
dbStatement.sqlConnection = dbConn;
var sql:String;
switch (insertCount) {
case 1 :
sql = "INSERT INTO Users VALUES(null,'MyName','MyPassword');";
break;
case 2 :
sql = "INSERT INTO Users VALUES(null,'qop','qop543');";
break;
case 3 :
sql = "INSERT INTO Users VALUES(null,'小黑','出運了');";
break;
}
dbStatement.text = sql;
dbStatement.addEventListener(SQLEvent.RESULT, doInsert);
dbStatement.execute();
}
執行第一次的結果 (由無到有建立DB):
執行第二次的結果 (取得資料秀在 DataGrid 上):
1 則留言:
你好!
我把你的程序试了一下,在AIR1.0下,结果老是不能编译通过!!!
提示 trace("Error Code:", err.code);这句中的err.code有误,不知怎么解决???
張貼留言