下载sql表的值以供离线重用 [英] download values of sql table for offline reuse

查看:28
本文介绍了下载sql表的值以供离线重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Flash 应用程序,它调用一个在线 php 文件以读取我的 SQL 表的一些值.

I've got a Flash app that calls an online php file in order to read some values of my SQL table.

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

So I've got a line like this in my AS3 code:

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

这在我的 php 中:

And this in my php :

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

问题:如果用户离线,他将无法访问这些值.

Problem : if the user is offline he can't access the values.

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

Is there way to download the SQL table with AS3 code (when the user have internet) in order to access it offline.

喜欢:

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 表?

And what should have sql_result_offline.php in order to access an offline SQL Table ?

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

谢谢,

推荐答案

对于 FLASH :

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

To save data locally with flash, you can use one of 3 manners : the Flash Player cache, a SharedObject, or a FileReference object. And for your local file, forget PHP and MySQL because we are speaking only about the data that you got ( json, xml, txt, ... ).

- Flash Player 缓存:

您应该知道,默认情况下,Flash Player 会将您文件的本地副本放入其缓存中.您可以将此本地副本用作数据的离线源,但这里不要忘记 Flash 播放器没有保存远程文件的最新版本,而是第一个版本,并且 http://www.example.com/data.php 不同于 http://www.example.com/data.php?123 即使是同一个文件!有关更多详细信息,请查看我对此问题的回答.

You should know that by default, flash player put a local copy of your file in its cache. You can use this local copy as an offline source of your data, but here don't forget that flash player didn't save the last version of your remote file but the first one and that 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 :

I don't know the size of your loaded data, but as Adobe said about SharedObject :

... 用于在用户计算机上读取和存储有限数量的数据...

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

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

I think that is not used for large files and it's not recommended to store files but some simple data. Of course, as a cookie for the browser, SharedOject needs user's authorization to write data to the hard drive, and user can delete it at any time.

- 文件引用:

我认为这是做您正在寻找的最佳方式.您应该知道要使用 FileReference 保存文件,您的用户会被邀请选择一个文件来保存数据并再次读取它.因此,如果您不希望任何用户与您的应用进行交互,请忽略这种方式.

I think this is the best manner to do what you are looking for. You should know that to save a file using FileReference, your user is invited to select a file for saving data and reading it in a second time. So if you don't want any user's interaction with your application, forget this manner.

FileReference 使用示例:

FileReference using example :

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();
}

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

This code will show every time a save dialog to the user, of course, it's just a sample, you have to adapt it to your needs ...

编辑

对于 AIR:

对于 AIR,我们不需要 FileReference 对象,而是使用 文件FileStream 对象来保存数据:

With AIR we don't need a FileReference object, instead we use File and a FileStream object to save data :

// 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);            
}

希望能帮到你.

这篇关于下载sql表的值以供离线重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆