Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML IndexedDB Insert

Posted in HTML Tutorial
Updated on Dec 15, 2023
By Mari Selvan
👁️ 90 - Views
⏳ 4 mins
💬 1 Comment
HTML IndexedDB Insert

Photo Credit to CodeToFun

How to insert in IndexedDB?

To insert data into IndexedDB, follow these steps:

  1. Open a connection to the database using indexedDB.open('databaseName', version)
  2. In the onupgradeneeded event, create an object store using createObjectStore('objectStoreName').
  3. In the onsuccess event, start a transaction with transaction('objectStoreName', 'readwrite').
  4. Access the object store using objectStore('objectStoreName').
  5. Use the add() method to insert data into the object store.

📄 Example

The following is a simple example to `Insert a data in IndexedDB`.

demo.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html>
<head>
  <title>IndexedDB Insert Example</title>
  <script>
    // Open a connection to the database
    const request = indexedDB.open('myDatabase', 1);

    request.onupgradeneeded = (event) => {
      const db = event.target.result;
      const objectStore = db.createObjectStore('myObjectStore', { keyPath: 'id' });
    };

    request.onsuccess = (event) => {
      const db = event.target.result;
      const transaction = db.transaction('myObjectStore', 'readwrite');
      const objectStore = transaction.objectStore('myObjectStore');
      
      const data = { id: 1, name: 'John Doe', age: 25 };
      const request = objectStore.add(data);

      request.onsuccess = () => {
        console.log('Data inserted successfully.');
      };

      transaction.oncomplete = () => {
        console.log('Transaction completed.');
        db.close();
      };
    };

    request.onerror = (event) => {
      console.error('Error opening database:', event.target.error);
    };
  </script>
</head>
<body>
  <!-- HTML content -->
</body>
</html>

In this example, we have an HTML file that includes a script section where the IndexedDB operations are performed.

The script starts by opening a connection to the database named myDatabase and creating an object store named myObjectStore. Then, it starts a transaction and inserts data { id: 1, name: 'John Doe', age: 25 } into the object store using the add() method.

When running this HTML file in a browser, the script will execute and perform the IndexedDB operations accordingly. You can check the browser's developer console to view the output and any potential errors.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
5 months ago

If you have any doubts regarding this article (HTML IndexedDB Insert) please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy