【问题标题】:angular2 ionic2 - how insert data request json into database sqliteangular2 ionic2 - 如何将数据请求 json 插入数据库 sqlite
【发布时间】:2017-02-03 00:11:19
【问题描述】:

我需要将一些 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();
                        })
                }
            //}

        });

【问题讨论】:

    标签: json sqlite angular ionic2


    【解决方案1】:

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

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

    【讨论】:

    • 数据库已打开
    • 原始堆栈跟踪:
    • ypeError: 无法在 SafeSubscriber._next (main.js:57339) 处读取 SafeSubscriber.__tryOrUnsub (main.js:45010) 在 SafeSubscriber.next (main.js:44959) 处未定义的属性“长度” ) 在 Subscriber._next (main.js:44912) 在 Subscriber.next (main.js:44876) 在 MapSubscriber._next (main.js:107682) 在 MapSubscriber.Subscriber.next (main.js:44876) 在 XMLHttpRequest。 XHRConnection.response.__WEBPACK_IMPORTED_MODULE_2_rxjs_Observable__.Observable.onLoad (main.js:55006) at t.invokeTask (polyfills.js:3) at Object.inner.inner.fork.onInvokeTask (main.js:35923)
    • 您遇到的错误似乎与您的 SQLIte 没有任何关系。看起来数据未定义,因此当您尝试获取“长度”属性时它会失败。我建议放置一个“if”来检查数据是否不长。我编辑了答案以反映这一点
    【解决方案2】:

    解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-15
      • 2018-11-18
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 2018-11-24
      • 1970-01-01
      相关资源
      最近更新 更多