TechTorch

Location:HOME > Technology > content

Technology

Creating a Local Offline Database Using JavaScript and SQLite

June 02, 2025Technology2190
How to Create a Local Offline Database Using JavaScript and SQLite Cre

How to Create a Local Offline Database Using JavaScript and SQLite

Creating a local offline database using JavaScript and MySQL typically involves using a combination of technologies since JavaScript alone does not directly interact with MySQL databases. However, you can achieve this by using a local server setup with Node.js and a client-side database like SQLite. Here’s a general approach using Node.js and SQLite which is more suitable for local offline storage.

Step 1: Set Up Your Environment

Install Node.js: Make sure you have Node.js installed on your machine. You can download it from

Create a New Project: Run the following commands in your terminal to create a new directory and enter it.

mkdir local-database
 cd local-database
 npm init -y

Install SQLite3:

npm install sqlite3

Step 2: Create a Simple SQLite Database

Create a JavaScript file, e.g., database.js.

const sqlite3  require('sqlite3').verbose();// Create a new database or open an existing oneconst db  new ('./localDatabase.db');// Create a table(`CREATE TABLE IF NOT EXISTS users (  id INTEGER PRIMARY KEY AUTOINCREMENT,  name TEXT NOT NULL,  email TEXT NOT NULL UNIQUE)`);// Insert sample dataconst stmt  (`INSERT INTO users (name, email) VALUES (?, ?)`);('John Doe', 'john@');('Jane Doe', 'jane@');();// Query the database(`SELECT * FROM users`, function(err, rows) {  console.log(rows);});// Close the database connection();

Step 3: Run Your Script

In your terminal, run the script:

node database.js

Step 4: Accessing the Database in a Web Application

If you want to access this database from a web application running in the browser, you would typically use a server-side application like the one you’ve created to interact with the database and expose an API.

Set Up an Express Server

Install Express:
npm install express

Modify your database.js to have the following content:

const express  require('express');const sqlite3  require('sqlite3').verbose();const app  express();const db  new ('./localDatabase.db');(express.json());// API endpoint to get users('/users', (req, res) > {  ('SELECT * FROM users', function(err, rows) {    res.json(rows);  });});// Start the serverconst PORT  process.env.PORT || 3000;(PORT, () > {  console.log(`Server running on port ${PORT}`);});

Run the server

node database.js

Access the API

You can use tools like Postman or your browser to access http://localhost:3000/users to see the user data.

Conclusion

By following these steps, you can create a local SQLite database using JavaScript with Node.js, allowing you to store and retrieve data offline. For a fully offline experience, you would typically use IndexedDB in the browser, but that would not involve MySQL directly. If you specifically need MySQL, you would have to run a local MySQL server, which goes beyond a purely offline setup.