2017-12-17 1 views
-2

Ich versuche SQLite Anfragen (INSERT und SELECT * FROM TABLE) mit IONIC zu tun. Ich suche Ihre Hilfe ...SQLite Anfragen mit IONIC

1) Ich habe eine Datenbank zu erstellen:

createDatabaseFile() { 
    this.sqlite.create({ 
     name: DATABASE_FILENAME, 
     location: 'default' 
    }) 
     .then((db: SQLiteObject) => { 
     this.db = db; 
     //Create table 'favories': 
     this.createTable("CREATE TABLE IF NOT EXISTS favories (ID INTEGER PRIMARY KEY NOT NULL, cityNameFavory VARCHAR(100));"); 
     console.log('DB well created.'); 
     }) 
     .catch(e => console.log(e)); 
    } 

Die Methode create unten:

createTable(sqlInstruction) { 
    this.db.executeSql(sqlInstruction, {}) 
    .then(
    () => { 
     console.log('OK : ' + sqlInstruction); 
    }) 
    .catch(e => console.log(e)); 
    } 

2) ich eine Stadt wie Favory mit AddFavorite hinzufügen (Stadt) Methode, die von .html aufgerufen:

<ion-input [(ngModel)]="cityName" placeholder="Add a city..."></ion-input> 
    <button ion-button icon-only (click)="addFavorite(cityName)"> 
     <ion-icon name="add"></ion-icon> 
    </button> 

kann ich das in cityname console.log bekommen, aber ich did'nt Erfolg einfügen diese Stadt in meinem Tisch. Nachfolgend finden Sie die AddFavorite Methode:

addFavorite(city) { 
    this.db.executeSql("INSERT INTO favories (cityNameFavory) VALUES(?);", {city}) 
    .then(() => { 
     alert('Ville ajoutée !'); 
    }) 
    .catch(e => console.log(e)); 

    } 

3) ich meine Daten nicht abrufen kann, wenn ich "x FROM SELECT" verwenden, auch wenn ich die Anzahl der Zeilen gut abrufen:

getData() { 
    this.db.executeSql("SELECT cityNameFavory FROM favories;", {}) 
    .then((data) => { 
     if(data == null) { 
     alert("Empty!") 
      } 

      if(data.rows) { 
     console.log("Number of lines = " + data.rows.length); 

       if(data.rows.length > 0) { 
        for(var i = 0; i < data.rows.length; i++) { 
      this.listFavories.push(data.rows.item(i).cityNameFavory); 
      alert(this.listFavories[i]); 
      } 

     } 
     } 
    }) 
    .catch(e => console.log(e)); 
    } 

und:

public listFavories: string[] = []; 

Hier finden Sie meine Details:

cli Pakete:

@ionic/cli-utils : 1.19.0 
ionic (Ionic CLI) : 3.19.0 

globale Pakete:

cordova (Cordova CLI) : 7.1.0 

lokale Pakete:

@ionic/app-scripts : 3.1.4 
Cordova Platforms : none 
Ionic Framework : ionic-angular 3.9.2 

System:

Android SDK Tools : 26.1.1 
Node    : v7.9.0 
npm    : 4.2.0 
OS    : Windows 10 

Antwort

-1

Schließlich modifizierte ich den Code wie folgt:

this.db.executeSql('INSERT INTO `table` (cityName) VALUES(\'' + city + '\')', {}) 
    .then(() => { 
     console.log("City added"); 
    }) 
    .catch(e => console.log(e)); 
Verwandte Themen