Use A Pre-Populated SQLite Database With NativeScript And Angular 2

I recently wrote about how to use a SQLite database within a NativeScript Android and iOS application that was built with Angular 2.  This was more or less a revisit to the vanilla NativeScript tutorial on the same subject I had written earlier in the year.  What happens when you have a massive amount of data that you’d like to save your user from needing to download before using your application?  Can a SQLite database be pre-populated and included within an application?

To keep the flow going, I figured it would be a good idea to demonstrate how to ship a NativeScript Angular 2 application with a pre-filled SQLite database rather than populating it on-the-fly.

To be fair, I wrote how to do this very thing in a vanilla NativeScript application.  While it can be accomplished in pretty much the same manner, I figured it would be a good idea to revisit this for clarity.

Let’s start by creating a fresh NativeScript project that makes use of Angular 2.  From the Command Prompt (Windows) or Terminal (Mac and Linux), execute the following:

The --ng tag above says to create an Angular 2 project.  While we’re adding the iOS build platform, you cannot actually build for iOS unless you’re using a Mac with Xcode installed.

With the project created, we need to add the SQLite plugin created by Nathanael Anderson.  This can be done by executing the following:

Before we go any further with the project, we need to come up with a SQLite database that contains some data.  There are plenty of tools floating around the internet for this, but my favorite is DB Browser for SQLite.

Download DB Browser and choose to create a file called populated.db after you open it and you’re prompted.

DB Browser for SQLite

With the application running, choose to create a new table with an id that is an auto incrementing primary key integer, a firstname that is a text value, and a lastname that is also a text value.  The name of this table should be person, but in reality, you can name it whatever you want as long as you remember it.

DB Browser for SQLite

With the table created, you need to add data into it.  This can be done by browsing for data and choosing to add a new record.  It is very important that you choose to write data when you’re done, otherwise any changes you make will not be saved.

DB Browser for SQLite

While our populated.db file probably doesn’t have many records in it, you could potentially have a lot.

When it comes to including this database file in our project, file placement is incredibly important.  Copy populated.db into the project’s app directory.  When we copy it via application code, the SQLite plugin will take the database file from the app directory and place it where it needs to be.

Now let’s take a look at how to use this pre-populated database within the NativeScript Angular 2 application.

Take a look at the following TypeScript component:

This component is part of what was created in the previous tutorial I wrote.  However, take a look at the following three lines:

The above three lines is all it takes to copy a pre-filled database from the project’s app directory into the appropriate Android and iOS working spaces.  If the database populated.db does not exist where appropriate it will be copied, otherwise it will be ignored.

Once the SQLite database has copied over, the constructor method will continue as normal.


You just saw how to deploy an NativeScript application that uses Angular 2 with a pre-populated SQLite database.  This is the Angular 2 version of the vanilla NativeScript tutorial I wrote on the same topic.  If you want to dive deeper into SQLite with Angular 2 in NativeScript, I strongly encourage you to check out my tutorial on the subject.

A video version of this article can be seen below.

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

  • Андрій Шевців

    Hello. I write app on nativescript(angular/typescript) and I need multiple tables in my sqlite database.
    I create service with code:

    import {Injectable} from “@angular/core”;
    let sqlLite = require(“nativescript-sqlite”);

    export class Database{
    private db: any;
    private readonly DB_FILE_NAME: string = “passSaverDb.db”;


    But when I try select data from passwordComplexity table I get error:
    EXCEPTION: Uncaught (in promise): Error: android.database.sqlite.SQLiteException: no such table: passwordComplexity (code 1): , while compiling: SELECT * FROM passwordComplexity ORDER BY id ASC LIMIT 1.

    My question is how I need to create multiple tables?

    • I don’t think you can execute multiple SQL statements in the same query. You should break your two table creation commands up.

  • Hardik

    Do you have any tutorial for using sqlite directly with Angular4/Typescript, without using NativeScript? I want to use it in Electron app

    • There is WebSQL, but that is unreliable and I think is being phased out. Electron should be able to use Node SQLite libraries.

      • Hardik

        I have tried using node sqlite library( but i am not able to make it work successfully.
        So after some research i am planning to use IndexDB as WebSQL is almost deprecated