【问题标题】:Connectng to remote database in Adobe AIR在 Adob​​e AIR 中连接到远程数据库
【发布时间】:2013-07-09 13:27:52
【问题描述】:

我正在开发一个 AIR 移动应用程序。我使用 Sqlite 作为我的本地数据库。现在我必须在位于远程服务器的集中式数据库中维护一些数据,即,我必须将一些数据从我的移动应用程序推送到远程数据库并进行检索。

我的问题是

  1. 我可以使用 sqlite 作为集中式数据库吗?

  2. 如何连接到服务器中的数据库。

提前致谢。

【问题讨论】:

  • 我建议您使用服务器端编程来访问我猜位于服务器上的远程数据库,因为它更安全,并且通常是一种更好的处理方式。
  • 感谢您的回复。您能告诉我 Adob​​e AIR 可以使用哪种服务器端编程语言。
  • 这并不取决于。您可以使用 PHP、.NET 或任何您喜欢的东西。然后,您可以使用 HTTP 请求将数据发送到服务器,服务器会将数据写入数据库并给您响应。
  • 谢谢吉奥。我会试试这个并发表评论。
  • 我可以用 Actionscript3 实现这个吗???目前我在 Flashbuilder 中使用 Actionscrip3。

标签: actionscript-3 sqlite air adobe


【解决方案1】:

我会尝试总结这个问题的答案。 SQLite 不是为服务而设计的,因此不能用作远程数据库,除非您只需要从中读取数据。

如果您只需要它来读取数据,您可以从服务器下载它,保存为文件,然后将其用作本地数据库。这种方法在这里解释得更好:Link

但是如果你想用它来写和读,你会在并发和数据使用方面遇到很多问题,所以这里不能选择使用 SQLite 数据库。在这里更好地解释:Link

这样做的正确方法是使用后端(服务器脚本或服务器程序),它会监听您的请求并根据它们采取行动。

例如,如果您想对用户进行身份验证,您可以向服务器发送类似这样的内容:

username: "Gio"
password: "123"
action: "login"

在 Actionscript 3 中调用如下所示:

import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.net.URLRequestMethod;

// create a URLLoader to POST data to the server
var loader:URLLoader = new URLLoader();
// server address where your script is listening
var request:URLRequest = new URLRequest("http://example.com");

// create and set the variables passed to the server script
var params:URLVariables = new URLVariables();
params.username = "Gio";
params.password = "123";
params.action = "login";
request.data = params;

// set the request type to POST as it's more secure and can hold more data
request.method = URLRequestMethod.POST;

loader.addEventListener(Event.COMPLETE, onPostComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, onPostFailed);
loader.load(request);

function onPostFailed(e:Event):void
{
// happens if the server is unreachable
trace("I/O Error when sending a POST request");
}

function onPostComplete(e:Event):void
{
// loader.data will hold the response received from the server
trace(e.target.data);
}

在服务器端,我建议使用 PHP,因为它更容易学习、简单并且与 MySQL 配合使用非常好。

在 PHP 中,您将有类似的东西来处理请求:

<?php

$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
if($_POST['action'] == "login")
{
    $receivedUsername=$_POST['username']; 
    $receivedPassword=$_POST['password']; 

    $sql="SELECT * FROM $tbl_name WHERE username='$receivedUsername' and password='$receivedPassword'";
    $result=mysql_query($sql);

    // Mysql_num_row is counting table row
    $count=mysql_num_rows($result);

    // If result matched $receivedUsername and $receivedPassword, table row must be 1 row
    if($count==1){
        echo "Authentication success";
    }
    else {
        echo "Wrong Username or Password";
    }
}
?>

起初这看起来有点复杂,但实际上并没有什么大不了的。您必须阅读有关 PHP 以及如何设置它的更多信息,因为我无法在这篇文章中提供所有详细信息。

【讨论】:

  • 非常感谢吉欧。你的回复很有帮助,我会努力的。
  • 如果我能提供更多帮助,请告诉我。如果对您有帮助,请接受。
【解决方案2】:

我在服务器端使用AMFPHP。 在 Flash 你调用服务器上的服务:

_net_connection = new NetConnection();
_net_connection.addEventListener(NetStatusEvent.NET_STATUS, connectionStatusHandler);
_net_connection.connect(_server_url);
_responder = new Responder(loginResult, null);
_net_connection.call("userSerivce2/login", _responder, _user_login, _user_passwword);

private function loginResult(result_object:Object):void {
    save_log('loginResult (' + result_object.status + ")");
    //rest of code
}

responder - 是 AS3 中的函数,您将在调用后执行 userSerivce2/login - 类 userSerivce2,方法登录 在这种情况下,您添加响应者 (_responder) 和其他参数,例如 _user_login、_user_passwword。

服务器 PHP:

public function login($user_name, $user_password) {
    //SELECT blablabla
    try {
        if ($logged) {
            $this->call_result['status'] = 0;
            throw new mysqli_sql_exception("Pomyślnie zalogowano."); 
        } else {
            $this->call_result['status'] = 3;
            throw new mysqli_sql_exception("Błąd logowania. Wprowadź poprawny login, oraz hasło."); 
        }
    } catch(Exception $e) {
        $this->call_result['error_message'] = $e->getMessage();
        return $this->call_result;
    }
    return $this->call_result; //method will return object
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2016-03-02
    • 2012-01-25
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多