我正在使用离子型制作todo应用,数据库是Sqlite。我的应用程序数据未插入,请帮助我如何在SQLite中为浏览器插入数据。
task.service.ts
import { Injectable } from '@angular/core';
declare var window;
@Injectable({
providedIn: 'root'
})
export class TaskService {
db: any;
constructor() {
let dbConfig={ name: 'my.db', location: 'default',version:'1',displayName:'fssfs',database_size:10000 };
this.db = window.openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
this.db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS TASK (jobId unique,title,description,date,)');
});
}
saveData(jobId,title,description,date){
this.db.transaction(function (tx) {
tx.executeSql('INSERT INTO TASK (jobId,title,description,date) VALUES (?, ?,?,?,?'), [jobId,title,description,date];
});
}
}
addtask.ts
addTask(){
this.service.saveData(this.todo.value.jobId,this.todo.value.title,this.todo.value.description,this.todo.value.date);
console.log(this.todo.value.jobId,this.todo.value.title,this.todo.value.description,this.todo.value.date);
}
答案 0 :(得分:0)
您的语法略有偏离。你有一个 '?'您的查询中的内容过多,加上您缺少一个“)”。
saveData(jobId, title, description, date) {
this.db.transaction(function (tx) {
tx.executeSql('INSERT INTO TASK (jobId,title,description,date) VALUES (?,?,?,?)',
[jobId, title, description, date],
function (tx, result) {
console.log('Query Success');
},
function (tx, error) {
console.log('Query Error: ' + error.message);
},
);
});
}