I have an SQLite instance in my app which works perfectly the first time the app is run but never again afterwards - I have no inkling what’s going on here.
Service:
import { Platform } from '@ionic/angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
constructor(private platform: Platform, private sqlite: SQLite) {
this.platform.ready().then(() => {
this.sqlite.create({
name: 'data.db',
location: 'default'
})
.then((db: SQLiteObject) => {
this.database = db;
this.database.executeSql('CREATE TABLE IF NOT EXISTS favouriteQuotes (id VARCHAR(255) NOT NULL, date VARCHAR(255), content VARCHAR(255), author VARCHAR(255), PRIMARY KEY(id))');
})
.catch((error) => {
console.log(error);
});
})
}
public async addFavourite(id: string, date: string, content: string, author: string): Promise<any> {
let data = [encodeURI(id), encodeURI(date), encodeURI(content), encodeURI(author)];
return this.database.executeSql(`INSERT INTO favouriteQuotes (id, date, content, author) VALUES (?, ?, ?, ?)`, data);
}
public async getQuote(id: string) {
return this.database.executeSql(`SELECT * FROM favouriteQuotes WHERE id = ?`, [encodeURI(id)]);
}
public async getFavourites(limit: number = 5, offset: number = 0) {
return this.database.executeSql(`SELECT * FROM favouriteQuotes LIMIT ? OFFSET ?`, [limit, offset])
}
public async removeFavourite(id: string) {
return this.database.executeSql(`DELETE FROM favouriteQuotes WHERE id = ?`, [encodeURI(id)]);
}
And on the page…
public isFavourite(): void {
this.quoteService.getQuote(this.quote.id)
.then((res) => {
this.favourite = (res.rows.length > 0);
})
.catch((error) => {
this.alertService.error("Unexpected issues when connecting to the favourites database. Please try again.");
})
}
public addFavourite(): void {
this.quoteService.addFavourite(this.quote.id, this.quote.date, this.quote.content, this.quote.author)
.then(() => {
this.isFavourite();
})
.catch((error) => {
console.log(error);
this.alertService.error('There was an error favouriting this quote. Please try again.');
})
}
public removeFavourite(): void {
this.quoteService.removeFavourite(this.quote.id)
.then(() => {
this.isFavourite();
})
.catch((error) => {
console.log(error);
this.alertService.error('Could not remove quote from favourites. Please try again.');
})
}
Any ideas? Apologies if it’s something stupid!
EDIT: There’s a chance this isn’t an SQLite issue. After the second load the buttons (which are just <button>
elements - not <ion-buttons>
) suddenly have a chrome style orange outline around them when they are pressed…