SQL表的下载值进行离线再利用 [英] download values of sql table for offline reuse

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

问题描述

我有一个Flash应用程序,以便阅读我的SQL表的一些值调用在线PHP文件。

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

所以我有这样一行在我的AS3 code:

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.

有没有办法下载SQL表AS3 code(当用户有上网),以便离线访问。

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:

要保存的数据在本地使用闪光灯,你可以使用3方式之一:Flash Player的高速缓存,<一个href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/SharedObject.html"相对=nofollow>共享对象,或<一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html"相对=nofollow>的FileReference 对象。而对于本地文件,忘了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播放器的缓存:

您应该知道,在默认情况下,Flash播放器把你的文件的本地副本,在它的缓存。你可以使用这个本地副本作为数据的离线源,但是在这里不要忘记,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.

- 共享对象:

我不知道你的加载的数据大小,但Adobe表示对共享对象:

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:

我认为这是你在找什么最好的方式。你应该知道,保存使用的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();
}

这code将显示每次保存对话框给用户,当然,这只是一个样本,你必须以使其适应你的需要......

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对象,而不是我们用的文件和<一href="http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/FileStream.html"相对=nofollow>的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);            
}

希望能有所帮助。

Hope that can help.

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

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