Sqlite queries inside the constructor are executing twice in nativescript?


#1

Hi,

I need to create multiple tables in local db using Nativescript-Sqlite plugin. When i tried, the queries written inside the constructor is executing twice. I found that by given alert for each table creation code. How should i rectify this?

` (new Sqlite(“my.db”)).then(db => {
database = db;
db.execSQL(“CREATE TABLE IF NOT EXISTS people1 (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)”).then(id => {
alert(“table1 insertd”); }, error => {
console.log(“CREATE TABLES ERROR”, error);
});

db.execSQL(“CREATE TABLE IF NOT EXISTS people2 (id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)”).then(id => {
alert(“table2 insertd”); }, error => {
console.log(“CREATE TABLE ERROR”, error);
});

    }, error => {
        console.log("OPEN DB ERROR", error);
    });

}`

My development environment- Nativescript with angular2
IDE - Visual Studio Code


#2

Have you checked if the constructor itself is getting executed twice for some reason by putting a breakpoint or console.log on the first line of your constructor? Sometimes when I’m debugging and the app starts up on my android device I’ve noticed that things happen twice.

I have similar database creation logic in my ngOnInit rather than my constructor and it seems to work fine. To be honest I’m not sure if it’s better to put it in the constructor or ngOnInit, but this stack gives some insight.