繁体   English   中英

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

[英]angular2 ionic2 - how insert data request json into database sqlite

我需要将请求中的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();
                        })
                }
            //}

        });

我看不到您打开该数据库

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?!");
           }
       }
    }
}

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

            }

        })
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM