nativescript-sqlite icon indicating copy to clipboard operation
nativescript-sqlite copied to clipboard

Problem in Deleting Database on iOS when having two or more databases

Open andreMariano90 opened this issue 6 years ago • 3 comments

I have an app that has two databases. Sometimes I need to delete one of them. When I delete one of them on Android, the application keeps running fine and the other database keeps running fine as well. When I delete one of them on iOS the other database stops working and looks corrupted.

andreMariano90 avatar Sep 11 '19 19:09 andreMariano90

If you can send me a demo app that demonstrates this; I'll see what is up.

NathanaelA avatar Sep 11 '19 21:09 NathanaelA

Found the problem... Here we go..

One of my database is called EVENTS and the other is called config.db

When I delete de EVENTS database, the config.db database is also removed.

Renaming it to events.db fixed the issue.

Here follows the code (Vue.js code)

<template>
    <Page>
        <ActionBar title="Welcome to NativeScript-Vue!"/>
        <StackLayout>
            <Button text="1 - Create the databases" @tap="createDatabases" />
            <Button text="2 - Delete Database 2" @tap="deleteOneDatabase" />
            <Button text="3 - Query Data from the database 1" @tap="queryDataFromDatabase" />
        </StackLayout>
    </Page>
</template>

<script >
    export default {
        data() {
            return {
                msg: 'Hello World!',
                database1: null,
                database2: null,
            }
        },
        methods: {
            createDatabases: function () {
                const Sqlite = require('nativescript-sqlite');
                const db1Exists = Sqlite.exists('config.db');

                /* eslint-disable no-new */
                new Sqlite('config.db', ((err, db) => {
                    if (err) {
                        alert('Error Creating database1')
                    } else {
                        db.resultType(Sqlite.RESULTSASOBJECT);
                        this.database1 = db;
                        if (!db1Exists) {
                            db.execSQL('CREATE TABLE CONFIG('
                                + ' KEY TEXT NOT NULL,'
                                + ' VALUE TEXT NOT NULL,'
                                + ' PRIMARY KEY (KEY) );');
                            db.execSQL("insert into CONFIG(KEY,VALUE) values('databaseName','db1');");
                            alert('Database 1 created')
                        }else{
                            alert('Database 1 already exists')
                        }
                    }
                }))

                const db2Exists = Sqlite.exists('EVENTOS');

                new Sqlite('EVENTOS', ((err, db) => {
                    if (err) {
                        alert('Error Creating database2')
                    } else {
                        db.resultType(Sqlite.RESULTSASOBJECT);
                        this.database2 = db;
                        if (!db2Exists) {
                            db.execSQL('CREATE TABLE CONFIG('
                                + ' KEY TEXT NOT NULL,'
                                + ' VALUE TEXT NOT NULL,'
                                + ' PRIMARY KEY (KEY) );');
                            db.execSQL("insert into CONFIG(NOME,VALOR) values('databaseName','db2');");
                            alert('Database 2 created')
                        }else{
                            alert('Database 2 already exists')
                        }
                    }
                }))


            },
            deleteOneDatabase: function () {
                const Sqlite = require('nativescript-sqlite');
                Sqlite.deleteDatabase('EVENTOS');
            },
            queryDataFromDatabase: function () {
                this.database1.all('select * from CONFIG', (err, resultSet) => {
                    if (err) {
                        alert('Error on query ' + err)
                        return;
                    }

                    alert('SUCCESS! Resultset: ' + JSON.stringify(resultSet))
                });

            },
        }
    }
</script>

andreMariano90 avatar Sep 12 '19 15:09 andreMariano90

I haven't tested this code -- but one thing that stands out is that you never closed the database. So it is very possible that this is confusing the sqlite driver, especially if you are creating another one the same name after words.

NathanaelA avatar Aug 30 '21 17:08 NathanaelA