下载 sql table 的值以供离线重用

download values of sql table for offline reuse

我有一个 Flash 应用程序调用在线 php 文件来读取我的 SQL table 的一些值。

所以我的 AS3 代码中有这样一行:

var urlReq:URLRequest = new URLRequest ("http://www.****.com/sql_result.php");

这在我的 php 中:

 $connection = mysql_connect("mysql***.perso", "test", "password") or die ("Couldn't connect to the server.");

问题:如果用户处于离线状态,他将无法访问这些值。

有没有办法用 AS3 代码(当用户有互联网时)下载 SQL table 以便离线访问它。

喜欢:

function onConnection(e:Event = null):void{
if(monitor.available)
            {
                trace("You are connected to the internet");
                read_php_online();
            }
            else
            {
                trace("You are not connected to the internet");
                read_php_offline();
            }

            monitor.stop();
        }

function read_php_offline():void{
    var urlReq:URLRequest = new URLRequest ("local/sql_result_offline.php");
..
..
}

什么应该 sql_result_offline.php 才能访问离线 SQL Table ?

 $connection = mysql_connect("LOCAL", "user", "password");

谢谢,

您有 flash swf、移动应用程序或 air 应用程序吗?

Storing local data

您可以使用文件作为数据库(如 csv),对于手机和空中,您可以使用本地 SQLite 数据库。

如果您有本机桌面应用程序 - 可以通过本机进程或本机扩展使用 mysql,但这并不那么容易..

  • 编辑:
    Working with local SQL databases in AIR
    [+] 您可以通过加密、启动时的密码等方式保护您的数据安全。
    [-] 它需要更多的代码
    (安装后创建数据库,定期同步,如果没有互联网连接则从本地数据库获取数据。)mysql 和 sqlite 也有一些差异(例如 "insert or update" sqlite 的语句)

对于闪存:

要使用 Flash 在本地保存数据,您可以使用以下 3 种方式之一:Flash Player 缓存、SharedObject, or a FileReference 对象。对于您的本地文件,请忘记 PHP 和 MySQL,因为我们只谈论您获得的数据(json、xml、txt、...)。

- Flash Player 缓存:

您应该知道,默认情况下,Flash Player 会将文件的本地副本放入其缓存中。您可以将此本地副本用作数据的离线来源,但请不要忘记 Flash Player 保存的不是远程文件的最后一个版本,而是第一个 http://www.example.com/data.php is different from http://www.example.com/data.php?123 even if it's the same file ! For more details about that, take a look on my answer of this question

- 共享对象:

我不知道您加载的数据的大小,但正如 Adob​​e 关于 SharedObject 所说:

... is used to read and store limited amounts of data on a user's computer ...

我认为不适合大文件,不建议存储文件,而是一些简单的数据。当然,作为浏览器的cookie,SharedOject向硬盘写入数据需要用户授权,用户可以随时删除。

- 文件参考:

我认为这是完成您正在寻找的事情的最佳方式。您应该知道,要使用 FileReference 保存文件,您的用户会被邀请 select 一个文件以保存数据并在第二次读取它。因此,如果您不希望任何用户与您的应用程序进行交互,请忘记这种方式。

FileReference 使用示例:

var local_file_name:String = 'local.data',
    file:FileReference = new FileReference(),
    local_file_filter:FileFilter = new FileFilter('local data file', '*.data'),
    remote_data_url:String = 'http://www.example.com/data.php',
    url_request:URLRequest,
    url_loader:URLLoader,       
    connected:Boolean = true;

if(connected){
    get_remote_data();
} else {
    get_local_data();
}

function get_remote_data(): void {
    //we use a param to be sure that we have always the last version of our file
    url_request = new URLRequest(remote_data_url + ('?' + new Date().getTime()));
    url_loader = new URLLoader();
    url_loader.addEventListener(Event.COMPLETE, on_data_loaded);
    url_loader.load(url_request);           
}

function get_local_data(): void {
    // show the select dialog to the user to select the local data file
    file.browse([local_file_filter]);
    file.addEventListener(Event.SELECT, on_file_selected);          
}

function on_data_loaded(e:Event): void {
    var data:String = e.target.data;
    // if the remote data is successfully loaded, save it on a local file 
    if(connected){
        // show the save dialog and save data to a local file
        file.save(data, local_file_name);
    }
    // use your loaded data
    trace(data);            
}

function on_file_selected(e:Event): void {
    file.addEventListener(Event.COMPLETE, on_data_loaded);
    file.load();
}

此代码每次都会向用户显示一个保存对话框,当然,这只是一个示例,您必须根据自己的需要进行调整...

编辑

空气:

使用 AIR,我们不需要 FileReference 对象,而是使用 File and a FileStream 对象来保存数据:

// for example, our local file will be saved in the same dir of our AIR app
var file:File = new File( File.applicationDirectory.resolvePath('local.data').nativePath ),
    remote_data_url:String = 'http://www.example.com/data.php',
    data_url:String = remote_data_url,
    url_request:URLRequest,
    url_loader:URLLoader,       
    connected:Boolean = true;

if(!connected){
    // if we are not connected, we use the path of the local file
    data_url = file.nativePath;     
}

load_data();

function load_data(): void {
    url_request = new URLRequest(data_url);
    url_loader = new URLLoader();
    url_loader.addEventListener(Event.COMPLETE, on_data_loaded);
    url_loader.load(url_request);           
}

function on_data_loaded(e:Event): void {
    var data:String = e.target.data;
    if(connected){          
        // save data to the local file
        var file_stream:FileStream = new FileStream();
            file_stream.open(file, FileMode.WRITE);
            file_stream.writeUTFBytes(data);
            file_stream.close();
    }
    trace(data);            
}

希望能帮到你。