Cordova - Zugriff auf Datenbank

  • JavaScript

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Radinator.

    Cordova - Zugriff auf Datenbank

    Guten Morgen!

    Ich suche nach einer Möglichkeit, wie ich in einer Cordova App (per JS) eine Datenbankabfrage mache. Leider finde ich nur Beispiele bei denen die Datenbank lokal vorliegt:
    docs.phonegap.com/en/2.7.0/cor…rage.md.html#openDatabase

    Kann mir irgendwer weiterhelfen wie ich auf eine "online" DB zugreifen kann?
    Bzw wie macht man sowas bei Apps richtig, wenn ich eine cross-platform mit Cordova entwickeln will?

    LG Radinator
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Kannst du mit evtl n Link zu einem Tutorial schicken?
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    Nicht wirklich.
    Mit normalen JavaScript/PHP würde das ganze in etwa wie folgt aussehen (ungetestet).

    Spoiler anzeigen

    JavaScript-Quellcode

    1. var request = new XMLHttpRequest();
    2. request.open('GET', 'https://example.com/script.php?a=42', true);
    3. request.addEventListener('load', function() {
    4. var data = JSON.parse(request.responseText);
    5. alert(data.x);
    6. });
    7. request.send();


    oder besser, falls du jQuery nutzt:

    JavaScript-Quellcode

    1. $.ajax('https://example.com/script.php', {a: 42}).done(function(data) {
    2. alert(data.x);
    3. });


    Und PHP:

    PHP-Quellcode

    1. <?php
    2. $value = isset($_GET['a']) ? $_GET['a'] : 0;
    3. try {
    4. $db = new PDO('mysql:host=127.0.0.1;dbname=foo;charset=UTF8;', 'username', 'password', [
    5. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    6. ]);
    7. $stmt = $db->prepare('SELECT x, y, z FROM table WHERE column = :value');
    8. $stmt->execute([':value' => $value]);
    9. $object = $stmt->fetchObject()
    10. header('Content-Type: application/json; charset=utf8');
    11. echo json_encode($object);
    12. }
    13. catch(Exception $e) {
    14. // Fehler...
    15. http_response_code(500);
    16. echo 'Internal server error';
    17. }



    Ist natürlich nur ein billiges Beispiel. Für eine richtige Anwendung benötigst du wohl noch eine Authentifizierung, richtige Fehlerbehandlung und und und.
    @3daycliff:
    Danke für deine Antwort! Hat mir sehr weitergeholfen.

    Da Ich einen Zugriff von meiner App auf meinem Handy auf meinen RaspberryPi machen will, bekomme ich eine Content Same Origin Policy Fehlermeldung.

    Bin jetzt aber mittlerweile drauf gekommen, wie ich das ganze lösen kann: JSONP

    Nochmal danke für deine Antwort ;D

    Lg Radinator
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell