Ionic 3本机SQLite插件未按预期工作
我用离子3。但下面的代码并没有按预期工作。i、 e.对于插入的数据,我看不到Ionic 3本机SQLite插件未按预期工作,sqlite,ionic-framework,ionic3,cordova-plugins,ionic-native,Sqlite,Ionic Framework,Ionic3,Cordova Plugins,Ionic Native,我用离子3。但下面的代码并没有按预期工作。i、 e.对于插入的数据,我看不到console.log()数据。看来我做错了。你能告诉我怎么走吗 注意:没有错误。只是不起作用 storeApiKeyInSqlite(key: string, name: string) { this.sqlite.create({ name: 'MyInvoices.db', location: 'default' }).then((db: SQLiteObject) =&
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));
}
Op的反馈: 这是我的工作解决方案:
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));
}
原始答案:
对于显式设置为非NULL
的主键
,不能插入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)
});
我知道这并不能解决您的问题,但这只是一个建议:使用TypeForm,它为我节省了大量的时间。对不起,我没有理解您的意思吗?我应该在哪里打字@FarabiAbdelwahedi指的是它的sql<代码>(Id INT PRIMARY KEY NOT NULL您不能在那里插入NULL我从这里得到了
4.创建添加新数据的新页面
。很抱歉,我现在无法测试您的解决方案,因为我们现在有API问题。然后是错误:{消息:“sqlite3\U prepare\U v2失败:表Apikeys有3列,但提供了2个值”,代码:5}代码:5消息:“sqlite3\u prepare\u v2失败:表Apikeys有3列,但提供了2个值”对象
@Sampath我建议设置主键自动递增
或将Id INT主键非空
更改为仅Id INT主键
自动递增对于SQLite
不是一个好的解决方案。请参见以下内容: