Integrate sqlite with Nodejs ! 🔥
Tags: webdev, sql, javascript, beginners
Published: 2024-03-20 08:53:38

Hey there!!👋🏻, fellow developers! Are you ready to supercharge your Node.js applications with the incredible power of SQLite? Let's dive into the exciting world of SQLite3 module and see how it seamlessly integrates with Node.js to create jaw-dropping applications!

## 1. Requiring the SQLite3 Module

To begin your journey with SQLite3, you first need to incorporate the module into your Node.js project. Simply use the require() function to import the sqlite3 module:

```
// Requiring the sqlite3 module
const sqlite3 = require('sqlite3');
```
Once you've included the module, you gain access of methods for seamless database operations.

## 2. Creating/Connecting to a Database

In SQLite, databases are represented as individual files. The sqlite3.Database() method facilitates the creation or connection to a database file:
```
// Creating or connecting to a database file
const db = new sqlite3.Database('./my_database.sqlite');
```
With this step, you establish a connection to your database, 


## 3. Fetching a Single Row: db.get() Method

When you need to retrieve a single row from your database, the db.get() method proves invaluable. It executes a query and returns the first row matching the specified criteria:

```
// Fetching the first row matching the query
db.get("SELECT * FROM users WHERE id = 1", (error, row) => {
    if (error) {
        console.error(error.message);
        return;
    }
    console.log(row);
});
```
With db.get(), you can pinpoint and extract specific data from your database it return the first match of occurrence.

## 4. Fetching Multiple Rows: db.all() Method

For scenarios requiring retrieval of multiple rows that meet certain conditions, the db.all() method comes to the rescue. It executes a query and returns all rows matching the specified criteria:

```
// Fetching all rows from the 'products' table
db.all("SELECT * FROM products WHERE category = 'electronics'", (error, rows) => {
    if (error) {
        console.error(error.message);
        return;
    }
    console.log(rows);
});
```

With db.all(), you can efficiently gather extensive datasets tailored to your application's needs.

## 5. Iterating Over Rows: db.each() Method

The db.each() method empowers you to iterate over each row returned by a query, enabling custom actions to be performed on individual rows:

```
// Performing an action on each row from the 'orders' table
db.each("SELECT * FROM orders", (error, row) => {
    if (error) {
        console.error(error.message);
        return;
    }
    console.log(row);
});
```
Utilizing db.each(), you can execute personalised operations on every row retrieved from your database.

## 6. Executing SQL Commands: db.run() Method

When it comes to executing SQL commands that don't return data, such as table creation or row insertion, the db.run() method is your go-to solution:
```
// Creating a new table
db.run("CREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT)");

// Inserting a new row into the table
db.run("INSERT INTO products (name) VALUES ('Laptop')");
```
With db.run(), you wield the power to manipulate your database schema and content effortlessly.

## Conclusion
Congratulations! 🎉  You've embarked on a journey to unlock the full potential of SQLite3 in your Node.js applications. Armed with an understanding of its powerful methods, you're equipped to tackle complex database tasks with confidence. So go ahead, leverage the magic of SQLite3!💻✨