0

i have the following problem. I designed my litesql database over 'DB Browser for SQLite' and I'm stuck as soon as a query gets executed. The functions I am exporting are getting imported and used in nativescript-vue.

Webpack also copies the database with *.sqlite ending to the device. The android version I use is 9.

The way I initialize my db is;

 var Sqlite = require("nativescript-sqlite");
 var db;

 export function init() {
    if (!Sqlite.exists("test.sqlite")) {
       Sqlite.copyDatabase("test.sqlite");
    }

   new Sqlite("test.sqlite", function(err, dbConnection) {
      if (err) {
          console.log(err);
          return;
      }
      db = dbConnection;
      console.log("connection successful")
   });
}

After running the function console shows 'connection successful'. The database is placed in the root of the app folder. That way it should pull the database? Besides I got another question. How could I hide the database in the production?

So the way I execute the query is:

export function xxxx(**parameter**) {
    db.execSQL(
        "select random_id from random_table where some_id = ?",
        **parameter**,
        function(err, result) {
            console.log("result 1: " + result + " err: " + err);
        }
    );
}

The output is:

JS: 'result 1: null err: null'

I'm not even sure if it opens the database in the right way?

9
  • Try using db.get for select statements. Besides what you mean by hiding database on production? If you like DB not to be part of source code then you should probably download it on the fly Or use paid version of the plugin which supports encryptions. Commented Sep 22, 2019 at 13:19
  • @manoj yeah exactly that was what I was asking for by hiding database on production. When I use db.get it tells me, my table doesnt exists. 'no such table' error Commented Sep 22, 2019 at 16:11
  • Then may be it's something specific to your DB, may be you can create a Playground Sample with a sample db file. Commented Sep 22, 2019 at 16:17
  • @Manoj I would say it's better when I create the DB through db.execSQL, because I have the whole create code of my database that is needed. Do you think I have the opportunity to save it as a file after creation? That way problems should be solved right. Commented Sep 22, 2019 at 21:14
  • What you mean by save it as a file? If you are talking about exporting the DB, yes it's possible. It may be easy to copy the DB post creation from iOS simulator. Commented Sep 23, 2019 at 6:18

1 Answer 1

1

If you just want to export DB file from Android / iOS, you may use nativescript-share-file plugin and pass the right path.

Android

const filePath = application.android.context
            .getDatabasePath("your-db-name.sqlite")
            .getAbsolutePath();

new ShareFile().open({
    path: filePath,
});

For iOS the path will be different,

iOS

const filePath = knownFolders.documents().getFile("your-db-name.sqlite").path;

new ShareFile().open({
    path: filePath,
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I solved using the pre-populated database already. I don't need to export it anymore but that would be also relevant for other features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.