Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

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

Open
andreMariano90 opened this issue Sep 11, 2019 · 3 comments

Comments

@andreMariano90
Copy link

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.

@NathanaelA
Copy link
Owner

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

@andreMariano90
Copy link
Author

andreMariano90 commented Sep 12, 2019

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>

@NathanaelA
Copy link
Owner

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants