简体   繁体   中英

Ionic 4 SQLite : then and catch does not run

I have created a database service in my ionic app, when i call the createDatabase function it works and shows the message that the database is created. But when i am calling the saveUser() or createTable() functions, nothing happened, i got no errors and no log messages.

NB : when calling the saveUser() i got the 'save save' message which is outside the two blocks (then and catch), so this tell me that the function is called successfully.

 import { Injectable } from '@angular/core'; import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx'; @Injectable({ providedIn: 'root' }) export class DatabaseService { private db: SQLiteObject; results = []; constructor(private sqlite: SQLite) { } public createDatabase(): void { this.sqlite.create({ name: 'ocp_fuits_database.db', location: 'default' }) .then((db: SQLiteObject) => { console.log('Database created'); this.db = db; this.createTables(); }) .catch(e => console.log(e)); } private createTables(): void { this.db.executeSql('CREATE TABLE IF NOT EXISTS `users` (`idUser` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `username` TEXT NOT NULL, `password` TEXT NOT NULL)', []) .then(() => { console.log('Table users created'); }) .catch(e => console.log(e)); } public saveUser(username: string, password: string): void { this.db.executeSql('INSERT INTO users (username, password) VALUES (\\'' + username + '\\', \\'' + password + '\\')', []) .then((db: SQLiteObject) => { console.log(username + password + ' saved'); }) .catch(e => console.log(e)); console.log('save save'); } public getUser(username: string): boolean { let b: boolean = false; this.db.executeSql('select * from users where username like %' + username + '%', []) .then((result) => { if (result.rows.length > 0) { b = true; } }) .catch (e => console.log(e)); return b; } public getData() { this.db.executeSql('select * from users', []) .then((data) => { for (let i = 0; i < data.rows.length; i++) { let item = data.rows.item(i); this.results.push(item); } }) .catch(e => { console.log(e); }) return this.results; } } 

In your create table SQL you are using grave accent (`) , that possibly is causing the failures, you don't need that for table name or column name. Try removing it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM