Learn Node SQLite

Opening A Database

Throughout this lesson, we're going to access an SQLite database with temperature data for countries over the last 150 years. We're going to take this data, collect it per year in a JavaScript object, average it, and save it into a new SQL database!

To get these two worlds to communicate, we will be importing a package into our JavaScript code. This package will allow us to open the channels of communication with our SQLite database. Once we do that, we can start writing SQL directly in our JavaScript!

The first order of business is to import the module that will facilitate this connection. Recall that to import a module in JavaScript we can use require() like so:

const sqlite3 = require('sqlite3');

The code above gives us a JavaScript object, called sqlite3 that we can interact with via methods. The first method we're going to use on sqlite3 is going to be the method that opens up a new database. In SQLite, a database corresponds to a single file, so the only argument required to open this database is the path to the file that SQLite will use to save the database.

const db = new sqlite3.Database('./db.sqlite');

This code will create a new database file, in the current folder, named db.sqlite. Then we'll have a database to interact with!

Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.