【问题标题】:Ionic 3 native SQLite plugin is not working as expectedIonic 3 本机 SQLite 插件未按预期工作
【发布时间】:2019-04-28 06:34:57
【问题描述】:

我使用 Ionic 3 native SQLite plugin。但下面的代码没有按预期工作。即我看不到插入数据的console.log() 数据。看来我在这里做错了。你能告诉我正确的方法吗?

注意:没有错误。只是不工作。

  storeApiKeyInSqlite(key: string, name: string) {
    this.sqlite.create({
      name: 'MyInvoices.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      db.executeSql('CREATE TABLE IF NOT EXISTS Apikeys(Id INT PRIMARY KEY NOT NULL, ApiKey NVARCHAR(100) NOT NULL, ApiName NVARCHAR(100) NULL)', [])
        .then(() => {
          db.executeSql('INSERT INTO Apikeys VALUES(NULL,?,?)', [key, name])
            .then(() => {
              db.executeSql('SELECT * FROM Apikeys', [])
                .then(res => {
                  if (res.rows.length > 0) {
                    console.log(res.rows.item(0).Id);
                    console.log(res.rows.item(0).ApiKey);
                    console.log(res.rows.item(0).ApiName);
                  }
                })
                .catch(e => {
                  console.log(e);
                });
            }).catch(e => {
              e => console.log(e)
            });
        }).catch(e => console.log(e));


    }).catch(e => console.log(e));
  }

【问题讨论】:

  • 我知道这不能解决您的问题,但这只是一个建议:使用 TypeORM,它为我节省了大量时间
  • 抱歉,没听懂?我应该在哪里输入? @FarabiAbdelwahed
  • 它的 sql..(Id INT PRIMARY KEY NOT NULL 你不能在那里插入 null

标签: sqlite ionic-framework ionic3 cordova-plugins ionic-native


【解决方案1】:

运维反馈:

这对我来说是可行的解决方案:

 storeApiKeyInSqlite(key: string, name: string) {
    this.sqlite.create({
      name: 'MyInvoices.db',
      location: 'default'
    }).then((db: SQLiteObject) => {
      db.executeSql('CREATE TABLE IF NOT EXISTS Apikeys(rowid INTEGER PRIMARY KEY,ApiKey NVARCHAR(100) NOT NULL, ApiName NVARCHAR(100) NULL)', [])
        .then(() => {
          db.executeSql('INSERT INTO Apikeys VALUES(NULL,?,?)', [key, name])
            .then(() => {
              db.executeSql('SELECT * FROM Apikeys', [])
                .then(res => {
                  if (res.rows.length > 0) {
                    console.log(res.rows.item(0).rowid);
                    console.log(res.rows.item(0).ApiKey);
                    console.log(res.rows.item(0).ApiName);
                  }
                })
                .catch(e => {
                  console.log(e);
                });
            }).catch(e => {
              console.log(e)
            });
        }).catch(e => console.log(e));
    }).catch(e => console.log(e));
  }

原答案:

您不能为明确设置为NOT NULLPRIMARY KEY 插入NULL

查询:

db.executeSql('INSERT INTO Apikeys(ApiKey,ApiName) VALUES(?,?)', [key, name])
            .then(() => {
              db.executeSql('SELECT * FROM Apikeys', [])
                .then(res => {
                  if (res.rows.length > 0) {
                    console.log(res.rows.item(0).Id);
                    console.log(res.rows.item(0).ApiKey);
                    console.log(res.rows.item(0).ApiName);
                  }
                })
                .catch(e => {
                  console.log(e);
                });
            }).catch(e => {
              e => console.log(e)
            });

【讨论】:

  • 我是从这里得到的 4. Create new Page of Adding New Datadjamware.com/post/59c53a1280aca768e4d2b143/…
  • 抱歉,我现在无法测试您的解决方案,因为我们现在有 API 问题。
  • 那么这就是错误:{message: "sqlite3_prepare_v2 failure: table Apikeys has 3 columns but 2 values were supplied", code: 5} code : 5 message : "sqlite3_prepare_v2 failure: table Apikeys has 3 columns but 2 values were supplied" __proto__ : Object
  • @Sampath 我建议设置primary key autoincrement 或将Id INT PRIMARY KEY NOT NULL 更改为Id INT PRIMARY KEY
  • AUTOINCREMENT 不是 SQLite 的好解决方案。请看这个:sqlitetutorial.net/sqlite-autoincrement
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-11
  • 2020-01-08
  • 2016-08-31
  • 2020-07-23
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多