简体   繁体   English

Ionic 4 SQLite:然后,catch不会运行

[英]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. 我在离子应用程序中创建了一个数据库服务,当我调用createDatabase函数时,该函数可以工作并显示创建数据库的消息。 But when i am calling the saveUser() or createTable() functions, nothing happened, i got no errors and no log messages. 但是当我调用saveUser()或createTable()函数时,什么也没发生,我没有错误,也没有日志消息。

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. 注意:当调用saveUser()时,我得到了“保存保存”消息,该消息位于两个块(然后是catch)之外,因此这告诉我该函数已成功调用。

 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. 在您的创建表SQL中,您使用的是grave accent (`) ,这可能会导致失败,而表名或列名则不需要。 Try removing it. 尝试将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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