2017-12-17 21 views
-2

IONIC을 사용하여 SQLite 요청 (INSERT 및 SELECT * FROM TABLE)을 수행하려고합니다.IONIC을 사용하는 SQLite 요청

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)); 
    } 

CreateTable에서 방법은 다음과 같습니다 : 나는

1) 나는 데이터베이스 생성 ... 당신의 도움을 요청

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

2)가 addFavorite를 사용 favory 같은 도시를 추가을 (도시) 메서드 .html :

<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> 

console.log에서 cityName을 얻을 수 있지만 삽입하지 못했습니다. 내 도시에있는이 도시.

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)); 
    } 

: 나는 잘 라인의 수를 검색 할 경우에도,

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

    } 

3) 나는 "FROM X 선택"을 사용할 때 내 데이터를 검색 할 수 없습니다 : addFavorite 방법을 검색 할 수 있습니다 그리고 :

public listFavories: string[] = []; 

내 세부 사항을 검색 할 수 있습니다 :

CLI 패키지 :

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

글로벌 패키지 :

cordova (Cordova CLI) : 7.1.0 

지역 패키지 :

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

시스템 :

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

답변

-1

마지막으로, 나는 다음과 같이 내 코드를 수정 :

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