如何访问代号为 1 的网络服务器上的 sqlite 数据库

How can I access sqlite database on a webserver in codename one

请问如何访问代号为 1 的网络服务器上的 sqlite 数据库?我只能使用数据库 API 访问设备上的数据库。为了在网络服务器上访问它,我认为是完全不同的事情。请问我需要一个片段代码。谢谢

使用下面的代码,未经测试,您可能需要调整它以满足您的需要。如果有问题请发表评论:

ConnectionRequest req = new ConnectionRequest() {
    @Override
    protected void handleException(Exception ex) {
        //handle error
    }
};
req.setUrl(YourURL);
req.setPost(true);
req.setHttpMethod("POST"); //Change to GET if necessary
req.setDuplicateSupported(true);
req.addArgument("argumentToSendThroughPostOrGet1", "value1");
req.addArgument("argumentToSendThroughPostOrGet2", "value2");
NetworkManager.getInstance().addToQueueAndWait(req);

if (req.getResponseCode() == 200) {
    Map<String, Object> out = new HashMap<>();
    Display.getInstance().invokeAndBlock(() -> {
        JSONParser p = new JSONParser();
        try (InputStreamReader r = new InputStreamReader(new ByteArrayInputStream(req.getResponseData()))) {
            out.putAll(p.parseJSON(r));
        } catch (IOException ex) {
            //handle error
        }
    });
    if (!out.isEmpty()) {
         List<Map<String, Object>> responses = (List<Map<String, Object>>) out.get("response");
         for (Object response : responses) {
             Map res = (Map) response;
             System.out.println(res.get("key"));
         }
    } else {
        //handle error
    }
} else {
    //handle error
}

测试JSON响应:

{
    "response": [
        {
            "key": "I was returned",
        }
    ]
}

编辑:

从 TextField 传递数据:

req.addArgument("argumentToSendThroughPostOrGet1", myTextField.getText());

根据您的评论,您可以阅读 PHP 中的这些参数,如下所示:

$var1 = $_POST["argumentToSendThroughPostOrGet1"];
$var1 = $_GET["argumentToSendThroughPostOrGet1"]; // if GET method is used in Codename One
//Or use $_REQUEST which supports both methods but not advisable to be used for production
...

并且您可以在 php 代码中正常使用这些变量。

MySql 查询的用法示例:

class Connection {

    function connect() {
        $mysqli = mysqli_init();
        $mysqli->real_connect("localhost", "username", "password", "databaseName") or die('Could not connect to database!');
        $mysqli->query("SET NAMES 'UTF8'");
        return $mysqli;
    }

    function close() {
        mysqli_close($this->connect);
    }

}


$connection = new Connection();
$mysqli = $connection->connect();
$mysqli->query("SELECT * FROM MyTable WHERE ColumnName LIKE '%$var1%' ORDER BY PrimaryKeyId ASC LIMIT 100");