Key Concepts

Review core concepts you need to learn to master this subject

sqlite3 Module

//requiring the sqlite3 module const sqlite3 = require('sqlite3');

Node.js and SQLite are separate entities, but both can be used together to create powerful applications. First, we need to link them together by requiring the sqlite3 module. Once we have required this module in our JavaScript, it will give us access to methods to perform various actions with the database.

Creating/Connecting To A Database

//requiring the sqlite3 module const sqlite3 = require('sqlite3');

In SQLite, a database corresponds to a single file. The sqlite3.Database() method can be used to create or connect to a single file.

db.get() Method

//requiring the sqlite3 module const sqlite3 = require('sqlite3');

Sometimes we only need one row from a database. The method db.get() allows us to fetch a single row from a database matching a query. If multiple rows match the query only one row matching the query will be returned.

db.all() Method

//requiring the sqlite3 module const sqlite3 = require('sqlite3');

A query is a statement that speaks to a database and requests specific information from it. The db.all() method allows us to execute a query that returns all rows with specific data. For example, we can use db.all() to return all rows from a table that has the pet as a cat.

db.each() Method

//requiring the sqlite3 module const sqlite3 = require('sqlite3');

Sometimes we want to perform an action every time a row is returned. Using the db.each() method we can do exactly that. db.each() takes a query and a callback function that it performs on each row returned from the query. db.each() can also take a second callback function, which will be called when all of the queries are completed and processed.

db.run() Method

//requiring the sqlite3 module const sqlite3 = require('sqlite3');

Sometimes we want to do more than just get a result from a database. The db.run() method holds SQL commands that do not return rows; such as commands that will allow us to create, update tables, or insert new rows.

Handling Errors

//requiring the sqlite3 module const sqlite3 = require('sqlite3');

sqlite3 uses Node.js error-first callback style.The first argument in the methods db.run(), db.each(), db.get(), and db.all() will always be an Error object. This object will return an error if it exists and return null if no errors are found.

Chevron Left Icon
Learn Node SQLite
Lesson 1 of 2
Chevron Right Icon
  1. 1
    One of the most essential skills as a programmer is being able to identify and utilize the appropriate tool for a specified task. In the context of database management, this will mean using SQL to …
  2. 2
    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 obje…
  3. 3
    In the previous exercise we were able to import the ‘sqlite3’ library and use that to open our SQLite database — so far so good! But we still haven’t retrieved any information from it. Since …
  4. 4
    db.all() is a useful tool to fetch all the data we have that meets certain criteria. But what if we only want to get a particular row? We could do something like this: db.all(“SELECT * FROM Dog”, …
  5. 5
    Now we know how to retrieve data from a database when we know exactly what we’re looking for. But we may not always know what values we will need to search for when writing our program. When we wri…
  6. 6
    Not all SQL commands return rows, and in fact, some essential SQL commands do not. If we INSERT a row or CREATE a TABLE we will not receive a row in response. To perform SQL commands that do not re…
  7. 7
    No one’s perfect. Code, like people, can make mistakes. This is OK! What’s important is that we learn how to handle our difficulties while keeping our composure. Handling errors is an important par…
  8. 8
    While learning JavaScript, you likely learned about the powerful method .forEach() that allows us to process every element in an array separately. Now we will use a similar method that will enable …
  9. 9
    So far we’ve managed to: 1. Query a database for weather records by location. 2. Reformat that data into a JavaScript object. 3. Manipulate that JavaScript object to find new, meaningful informati…
  10. 10
    By default, the commands we issue to our database run in parallel. Every request we make gets sent to the database — which processes them all as quickly as it can, regardless of the order i…
  11. 11
    Wow, we were able to take data from a source, manipulate it for our needs, and save it separately. We’ve managed to set up a database, create tables, insert data, and modify it — all within J…
  1. 1
    Code Challenge 1
  2. 2
    Code Challenge 2
  3. 3
    Code Challenge 3
  4. 4
    Code Challenge 4
  5. 5
    Code Challenge 5
  6. 6
    Code Challenge 6
  7. 7
    Code Challenge 7
  8. 8
    Code Challenge 8
  9. 9
    Code Challenge 9
  10. 10
    Code Challenge 10
  11. 11
    Code Challenge 11
  12. 12
    Code Challenge 12
  13. 13
    Code Challenge 13
  14. 14
    Code Challenge 14
  15. 15
    Code Challenge 15
  16. 16
    Code Challenge 16
  17. 17
    Code Challenge 17
  18. 18
    Code Challenge 18
  19. 19
    Code Challenge 19

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo