使用as3在数据库中保存数据时遇到问题

时间:2017-08-05 08:00:24

标签: mysql sql actionscript-3 flash

我创建了一个简单的应用程序,使用只有两个字段(id(自动增量)和名称)的数据库来保存和加载数据。该数据库名为test1,并有一个名为test的表,其中包含两个字段idname

当我尝试保存一个简单的值时,我收到消息

  

INSERT错误:SQLError:'错误#3115:SQL错误。',详情:'no such table: 'test'',   operation:'execute',detailID:'2013'event.error.message:错误#3115:SQL错误。 event.error.details:no such table: 'test'

我已经创建了数据库,并将其保存在与Flash(.swf)文件相同的文件夹中。

这是我使用的代码:

import fl.data.DataProvider;
import flash.data.SQLResult;
import flash.data.SQLConnection;
import flash.filesystem.File;
import flash.data.SQLStatement;
import flash.data.SQLConnection;

var conn: SQLConnection;
var createStmt: SQLStatement;
var insertStmt: SQLStatement;
var insertStmt2: SQLStatement;
var selectStmt: SQLStatement;

var insert1Complete: Boolean = false;
var insert2Complete: Boolean = false;

loadBtn.addEventListener(MouseEvent.CLICK, getData);
SaveBtn.addEventListener(MouseEvent.CLICK, addData);
init();

function init(): void {
    conn = new SQLConnection();
    trace("opening database");
    var dbFile: File = File.applicationStorageDirectory.resolvePath("test1.db");
    conn.openAsync(dbFile);
}

function addData(event: MouseEvent): void {
    trace("Adding data to table");
    insertStmt = new SQLStatement();
    insertStmt.sqlConnection = conn;
    var sql: String = "";
    sql += "INSERT INTO test (id, name)";
    sql += "VALUES ('John')";
    insertStmt.text = sql;
    insertStmt.execute();
}


function getData(event: MouseEvent): void {
    trace("Loading data");
    selectStmt = new SQLStatement();
    selectStmt.sqlConnection = conn;
    var sql: String = "SELECT id, name";
    selectStmt.text = sql;
    selectStmt.addEventListener(SQLEvent.RESULT, selectResult);
    selectStmt.addEventListener(SQLErrorEvent.ERROR, selectError);
    selectStmt.execute();
}

function selectResult(event: SQLEvent): void {
    trace("Data loaded");
    selectStmt.removeEventListener(SQLEvent.RESULT, selectResult);
    selectStmt.removeEventListener(SQLErrorEvent.ERROR, selectError);
    var result: SQLResult = selectStmt.getResult();
    resultsGrid.dataProvider = new DataProvider(result.data);
}

function selectError(event: SQLErrorEvent): void {
    status.text = "Error loading data";
    selectStmt.removeEventListener(SQLEvent.RESULT, selectResult);
    selectStmt.removeEventListener(SQLErrorEvent.ERROR, selectError);
    trace("SELECT error:", event.error);
    trace("event.error.message:", event.error.message);
    trace("event.error.details:", event.error.details);
}

0 个答案:

没有答案