我试图保持简单,现在只是添加和显示,现在不需要删除条目或任何东西。任何帮助是极大的赞赏!
package
{
import flash.data.SQLConnection;
import flash.data.SQLResult;
import flash.data.SQLStatement;
import flash.events.Event;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;
import flash.net.URLLoader;
import flash.net.URLRequest;
import screens.LevelSelection;
public class Database
{
public var sqlConnection:SQLConnection;
private var db:File;
private var dbStatus:String= "";
private var dataResult:Object;
private var playersList:Array;
public var selectAllSQL:SQLStatement;
public function openDB():void
{
db = File.desktopDirectory.resolvePath("Players.db");
sqlConnection = new SQLConnection();
sqlConnection.addEventListener(SQLErrorEvent.ERROR, onSQLError);
if(db.exists)
{
sqlConnection.addEventListener(SQLEvent.OPEN, onOpenDB);
trace("DB Found");
}
else
{
sqlConnection.addEventListener(SQLEvent.OPEN, onCreateDb);
}
sqlConnection.openAsync(db);
}
private function onSQLError(event:SQLErrorEvent):void
{
var error:String = "Error id: "
+ event.error.errorID + "\nDetails: " + event.error.details;
dbStatus += event + "Error";
}
private function onOpenDB(event:SQLEvent):void
{
dbStatus += "The database was opened...";
if(SQLConnection(event.target).connected)
{
trace(dbStatus);
}
}
public function onCreateDb(event:SQLEvent):void
{
dbStatus += "The database was created...";
createPlayerTable();
trace(dbStatus);
}
Im not sure if my syntax is right for these statements. I tried to trace a lot of things out so that i can see the results in debugging
public function createPlayerTable():void
{
var sqlText:String =
"CREATE TABLE IF NOT EXISTS Players(NAME TEXT, SCORE INTEGER)";
var createTableSQL:SQLStatement = new SQLStatement();
createTableSQL.sqlConnection = sqlConnection;
trace("Create Table Connection: "
+ createTableSQL.sqlConnection.connected);
createTableSQL.addEventListener(SQLEvent.RESULT, onPlayersTable);
createTableSQL.addEventListener(SQLErrorEvent.ERROR, onSQLError);
createTableSQL.text = sqlText;
createTableSQL.execute();
trace(createTableSQL.getResult());
}
public function onPlayersTable(event:SQLEvent):void
{
dbStatus += "The Players table was created";
trace(dbStatus);
}
public function selectPlayers():void
{
var sqlText:String = "SELECT * FROM Players";
selectAllSQL = new SQLStatement();
selectAllSQL.sqlConnection = sqlConnection;
selectAllSQL.addEventListener(SQLErrorEvent.ERROR, onSQLError);
selectAllSQL.text = sqlText;
selectAllSQL.execute();
handleSelection(selectAllSQL);
}
public function handleSelection(event:SQLStatement):void
{
onSelectPlayers(selectAllSQL);
}
public function onSelectPlayers(event:SQLStatement):void
{
trace("select called");
var result:SQLResult = event.getResult();
if(result.data)
{
for(var i:int = 0; i < result.data.length; i++)
{
screens.LevelSelection.output.text +=
"Name: " + result.data[i]["NAME"] + ", " +
"Score: " + result.data[i]["SCORE"];
}
}
}
我见过几个人使用 @ 符号和几个人使用 : 来指定插入的值。
public function addPlayer():void
{
var sqlText:String =
"INSERT INTO Players(NAME, SCORE) Values(@name, @score)";
var addPlayerSQL:SQLStatement = new SQLStatement();
addPlayerSQL.sqlConnection = sqlConnection;
trace("ADD PLAYER CONNECTION: " + addPlayerSQL.sqlConnection.connected);
addPlayerSQL.addEventListener(SQLErrorEvent.ERROR, onSQLError);
addPlayerSQL.text = sqlText;
addPlayerSQL.parameters["@name"] = screens.LevelSelection.input.text;
addPlayerSQL.parameters["@score"] = 0;
addPlayerSQL.execute();
trace(addPlayerSQL.getResult());
trace(screens.LevelSelection.input.text);
trace("Player added");
selectPlayers();
}
}
}