如何在Ionic中创建数据库和表,插入值并从表中获取值?

时间:2019-12-17 12:20:58

标签: angular sqlite ionic4

我想创建一个SQLite数据库,然后在表格广告中插入一些值以获取它们并将它们打印到Ionic中的屏幕上。将值插入表时出现错误。请帮我解决一下这个。我已经在下面附加了home.page.ts代码。

import { Component } from '@angular/core';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
import { Platform } from '@ionic/angular';
@Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],})
 export class HomePage {
    data: any = [];

    constructor(
    private platform: Platform,
    private sqlite: SQLite) {
     this.sqlite.create({
     name:'users.db',
     location:'default'
     }).then((db:SQLiteObject)=>{
     db.executeSql('CREATE TABLE IF NOT EXISTS user(pid INTEGER, name varchar(255))',[])
    .then(r=> {db.executeSql('INSERT INTO user(pid,name) VALUES (1,"henry")',[])
    .then(res=>{
     db.executeSql('SELECT * FROM user',[])
     .then(res => {
       this.data = [];
      for(var i=0; i<res.rows.length; i++) {
        this.data.push({pid:res.rows.item(i),name:res.rows.item(i)})
      }
     })
     .catch(e=> alert("error while fetching"))

     })
     .catch(e=>{alert('error while inserting')})
     })
     .catch(e=>{alert('error while creating db')});

     }).catch(e=> alert("error"));
     }

     }

0 个答案:

没有答案