1

我目前正在关注一个培训视频来学习 Flex,并且我正在尝试尽可能接近视频说明,我一直在尝试找出如何解决错误

错误 #2044:未处理的 SQLErrorEvent:。errorID=3115,操作=执行,消息=错误 #3115:SQL 错误。,详细信息=没有这样的表:'存档列表'

该表应根据培训使用此代码创建。

<fx:Script>
    <![CDATA[
        import mx.utils.OnDemandEventDispatcher;

        public var ArchiveFile:File;
        public var dbConnection:SQLConnection;



        private function createDatabase():void
        {
            ArchiveFile = File.applicationStorageDirectory.resolvePath("archive.db");
            dbConnection = new SQLConnection();
            dbConnection.addEventListener(SQLEvent.OPEN, onDatabaseOpen);
            dbConnection.addEventListener(SQLEvent.CLOSE, onDatabaseClose);

            dbConnection.openAsync(ArchiveFile);
        }

        private function onDatabaseOpen(evt:SQLEvent):void
        {
            trace("Database is now open");
            var statement:SQLStatement = new SQLStatement();
            statement.sqlConnection = dbConnection;
            statement.text = "CREATE TABLE IF NOT EXISTS archivelist (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT , journal TEXT)";

            statement.execute();

            }       

        private function onDatabaseClose(evt:SQLEvent):void
        {
            trace("Database is now closed");
        }

        private function saveJournal():void
        {

            var statement:SQLStatement = new SQLStatement();
            statement.sqlConnection = dbConnection;
            statement.text = "INSERT INTO archive (title , journal) VALUES ('"+title_txt.text+"','"+journal_txt.text+"')";

            statement.execute();

            title_txt.text = "";
            journal_txt.text = "";

        }


    ]]>

然后使用此代码在另一个视图上调用

<fx:Script>
    <![CDATA[
        import flash.data.SQLConnection 
        import flash.filesystem.File
        import mx.collections.ArrayCollection;

        public var ArchiveFile:File
        public var dbConnection:SQLConnection;
        public var selectQuery:SQLStatement;

        private function createDatabase():void
        {
            ArchiveFile = File.applicationDirectory.resolvePath("archive.db");
            dbConnection = new SQLConnection();
            dbConnection.addEventListener(SQLEvent.OPEN, onDatabaseOpen);
            dbConnection.addEventListener(SQLEvent.CLOSE, onDatabaseClose);

            dbConnection.openAsync(ArchiveFile)
        }

        private function onDatabaseOpen(evt:SQLEvent):void
        {
            trace("Database is now open");
            selectQuery = new SQLStatement();
            selectQuery.sqlConnection = dbConnection;
            selectQuery.text = "SELECT * FROM archivelist ORDER BY title ASC";
            selectQuery.addEventListener(SQLEvent.RESULT, onQueryResult);   

            selectQuery.execute();  
        }

        private function onQueryResult(evt:SQLEvent):void
        {
            archivefiles.dataProvider = new ArrayCollection(selectQuery.getResult().data);
        }

        private function onDatabaseClose(evt:SQLEvent):void
        {
            trace("Database is now Closed");
        }

    ]]>

重要的是要注意,在视频中,培训师在名为 database.db 的视图下有一个蓝色文件,他们没有提供这个文件或如何制作它。为了我自己的方式,我已将我的数据库重命名为归档,并将我的表重命名为归档列表。我的最终目标是制作 1 个可供多个视图访问/写入的数据库。与最佳实践,所以我试图尽可能接近培训。我非常感谢提供的任何帮助。如果有人觉得这是一个新问题,我提前道歉,我已经在谷歌上研究了这个问题,并用训练视频检查了每个角色几个小时。我发现每个人似乎都有不同的方式,我正在尝试采用最佳实践方式。

谢谢你

4

1 回答 1

1

始终使用异步模式进行数据库交互,以便您的 UI 永远不会冻结。

错误 #2044:未处理的 SQLErrorEvent。由于您不应该打开异步模式,它应该是同步模式(我认为您尝试在同步模式下工作)

dbConnection.open(ArchiveFile); //  not openAsync(ArchiveFile);

或者您需要像这样处理 SQLErrorEvent

dbConnection.addEventListener(SQLErrorEvent.ERROR, onDatabaseCloseError);

最好的办法 :

1) 你需要阅读关于 DAO Pattern(Sun Java)

2)为您的整个应用程序使用单一连接,以便让您的 sqlconnection 在您的所有 DAO 类中可见(Sqlite 不支持并发,因此使用单一连接)。

3) 如果使用连接池,性能会提高,但会出现数据库文件当前锁定等问题。

4)使用参数化查询避免SQL注入。(也提高性能)

如果您想要示例应用程序,请点击此链接http://coenraets.org/blog/2008/02/salesbuilder-for-air-10/ 这些家伙为 sql 查询提供了出色的实用程序,还确保有 2 个版本尝试获取异步数据库操作 Salesbuilder 应用程序

请复制并粘贴它。它将按您的预期工作。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)">

<fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.events.FlexEvent;
        protected function btnGetArchiveList_clickHandler(event:MouseEvent):void
        {
            getArchiveList();
        }

        protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
        {
            createDatabase();               
        }

        private var ArchiveFile:File
        private var dbConnection:SQLConnection;
        private var selectQuery:SQLStatement;

        private function createDatabase():void
        {
            ArchiveFile = File.applicationStorageDirectory.resolvePath("archive.db"); //Note here best practices to create db file here applicationStorageDirectory
            dbConnection = new SQLConnection();
            dbConnection.addEventListener(SQLEvent.OPEN, onDatabaseOpen);
            dbConnection.addEventListener(SQLEvent.CLOSE, onDatabaseClose);
            dbConnection.addEventListener(SQLErrorEvent.ERROR, onDatabaseError);
            dbConnection.openAsync(ArchiveFile)
        }

        protected function onDatabaseError(event:SQLErrorEvent):void
        {
            trace(event.error.getStackTrace());
        }

        private function onDatabaseOpen(evt:SQLEvent):void
        {
            trace("Database is now open");
            var statement:SQLStatement = new SQLStatement();
            statement.sqlConnection = dbConnection;
            statement.text = "CREATE TABLE IF NOT EXISTS archivelist (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT , journal TEXT)";
            statement.addEventListener(SQLEvent.RESULT, onTableCreate_ResultHandler);   
            statement.addEventListener(SQLErrorEvent.ERROR, onDatabaseError);
            statement.execute();;
        }

        protected function onTableCreate_ResultHandler(event:SQLEvent):void
        {
            trace("Table created successfully") // Here after only you need to call all query like getArchiveList()
        }

        private function getArchiveList():void
        {
            trace("Getting ArchiveList");
            var selectQuery:SQLStatement = new SQLStatement();
            selectQuery.sqlConnection = dbConnection;
            selectQuery.text = "SELECT * FROM archivelist ORDER BY title ASC";
            selectQuery.addEventListener(SQLEvent.RESULT, onQueryResult);   
            selectQuery.addEventListener(SQLErrorEvent.ERROR, onDatabaseError);   
            selectQuery.execute();
        }

        private function onQueryResult(event:SQLEvent):void
        {
            archivefilesList.dataProvider = new ArrayCollection(((event.currentTarget) as SQLStatement).getResult().data);
        }

        private function onDatabaseClose(evt:SQLEvent):void
        {
            trace("Database is now Closed");
        }

        private function saveJournal():void
        {
            var statement:SQLStatement = new SQLStatement();
            statement.sqlConnection = dbConnection;
            statement.text = "INSERT INTO archivelist (title , journal) VALUES ('"+txtTitle.text+"','"+txtJournal.text+"')"; //Note here archivelist table name
            statement.addEventListener(SQLEvent.RESULT, onInsertQuery_ResultHandler);   
            statement.addEventListener(SQLErrorEvent.ERROR, onDatabaseError);   
            statement.execute();
            txtTitle.text = "";
            txtJournal.text = "";

        }

        protected function btnSave_clickHandler(event:MouseEvent):void
        {
            saveJournal();              
        }

        protected function onInsertQuery_ResultHandler(event:SQLEvent):void
        {
            var isInserted:Boolean = ((event.currentTarget) as SQLStatement).getResult().rowsAffected > 0;

            if(isInserted)
                trace("inserted successfully")
            else
                trace("insert failed");

        }

    ]]>
</fx:Script>

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

<s:layout>
    <s:VerticalLayout/>
</s:layout>
<s:TextInput id="txtTitle"/>
<s:TextInput id="txtJournal"/>
<s:Button id="btnSave" label="Save" click="btnSave_clickHandler(event)"/>

<s:Button id="btnGetArchiveList" label="Get Archive" click="btnGetArchiveList_clickHandler(event)"/>
<s:List id="archivefilesList" labelField="title" width="200" height="300"/>

</s:WindowedApplication>
于 2012-11-09T05:52:33.677 回答