Ionic 3 本机 SQLite 插件未按预期工作
Ionic 3 native SQLite plugin is not working as expected
我使用 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));
}
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));
}
原回答:
您不能为明确设置为 NOT NULL
的 PRIMARY 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)
});
我使用 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));
}
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));
}
原回答:
您不能为明确设置为 NOT NULL
的 PRIMARY 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)
});