angular2 ionic2 - 如何将数据请求json插入数据库sqlite

时间:2017-02-01 18:17:35

标签: json sqlite angular ionic2

我需要将JSON格式的一些数据从请求插入到我的应用程序中的SQLite数据库中。数据库正在运行,表存在,但我不知道如何将此函数插入​​表中。 以下是我的代码到目前为止。为什么不进行INSERT?

synchronizeData() {
    let db = new SQLite();

    let loader = this.loadingCtrl.create({
        content: "Sincronizando..."
    });
    loader.present();
    this.account.allAccounts()
        .subscribe(data => 
        {
            this.list = [];
            //if (data.rows.length > 0) {
            console.log(data.length);
                for (var i = 0; i < data.length; i++) {

                    this.AccountId = data.rows.item(i).Id;
                    this.AccountIdentification = data.rows.item(i).Identification;
                    this.AccountActive = data.rows.item(i).Active;
                    this.AccountEditionUserId = data.rows.item(i).EditionUserId;
                    this.AccountEditionDateTime = data.rows.item(i).EditionDateTime;
                    this.AccountAccountTypeId = data.AccountType.rows.item(i).Id;
                    this.AccountComplement = data.rows.item(i).Complement;
                    this.AccountPriceListId = data.rows.item(i).PriceListId;
                    this.AccountColor = data.rows.item(i).Color;
                    this.AccountReferenceKey = data.rows.item(i).ReferenceKey;

                    if (data.rows.item(i).Active == 'true') { this.AccountActive = 1 } else { this.AccountActive = 0 }
                    //this.results.push({ name: data.rows.item(i).name });
                    this.db.executeSql("INSERT INTO Accounts" +
                        "(Id, SId, Identification, Active, EditionUserId, EditionDateTime, AccountTypeId, Complement, PriceListId, Color, ReferenceKey)" +
                        "VALUES(" + this.AccountId + ", 0 ," + this.AccountIdentification + " , " + this.AccountActive + " , " + this.AccountEditionUserId + "," +
                        this.AccountEditionDateTime + ", " + this.AccountAccountTypeId + "," + this.AccountComplement + "," + this.AccountPriceListId + "," +
                        this.AccountColor + "," + this.AccountReferenceKey +
                        ")", []).then((data) => {
                            console.log("insert", data);

                        }, (error) => {
                            console.log("ERROR: " + JSON.stringify(error));
                        },
                        () => {
                            loader.dismiss();
                        })
                }
            //}

        });

2 个答案:

答案 0 :(得分:0)

我没有看到你打开那个数据库

synchronizeData() {
    let db = new SQLite();
    db.openDatabase({
      name: "data.db",
      location: "default"
    }).then(() => {
      let loader = this.loadingCtrl.create({
       content: "Sincronizando..."
        });
       loader.present();
       this.account.allAccounts()
       .subscribe(data => 
       {
           if(data){

              this.list = [];
              /* the rest of your code */
           } else {
             console.log("oops! data is undefined?!");
           }
       }
    }
}

答案 1 :(得分:0)

溶液

insertIntoTable(query: any) {
        this.database.openDatabase({
            name: this.nameDb,
            location: this.locationDb
        }).then(() => {
            this.database.transaction((tx) => {
                this.database.executeSql(query, {}).then((data) => {

                }, (error) => {
                    console.error("Unable to execute sql", error);
                }), (error) => {
                    console.error("Unable to open database", error);
                }
            })
        });
    }

这是一个解决方案

synchronizeData() {

    this.querys.selectAllTablesExists;

    let loader = this.loadingCtrl.create({
        content: "Sincronizando..."
    });
    loader.present();


    this.AccountService.allAccounts()
        .subscribe(data => {
            this.list = [];
            if (data.length > 0) {
                for (var i = 0; i < data.length; i++) {

                    this.AccountId = data[i].Id;
                    this.AccountIdentification = data[i].Identification;

                    this.AccountEditionUserId = data[i].EditionUserId;
                    this.AccountEditionDateTime = data[i].EditionDateTime;
                    this.AccountAccountTypeId = data[i].AccountType.Id;
                    this.AccountComplement = data[i].Complement;
                    this.AccountPriceListId = data[i].PriceListId;
                    this.AccountColor = data[i].Color;
                    this.AccountReferenceKey = data[i].ReferenceKey;

                    if (data[i].Active == true) { this.AccountActive = 1 } else { this.AccountActive = 0 }
                    this.querys.insertIntoTable("insert or replace into Account (Id, SId, Identification, Active, EditionUserId, EditionDateTime, AccountTypeId, Complement, PriceListId, Color, ReferenceKey) VALUES('" + this.AccountId + "', '0' ,'" + this.AccountIdentification + "' , '" + this.AccountActive + "' , '" + this.AccountEditionUserId + "','" + this.AccountEditionDateTime + "', '" + this.AccountAccountTypeId + "','" + this.AccountComplement + "','" + this.AccountPriceListId + "','" + this.AccountColor + "','" + this.AccountReferenceKey + "')")

                }

                loader.dismiss();

            }

        })
    }