Comprehensive Guide to UDB Universal Database Library

Introduction to UDB

UDB (Universal Database Library) is a robust and versatile database library that allows developers to interact effortlessly with various database management systems. In this comprehensive guide, we explore the powerful features of UDB and provide dozens of useful API examples along with an app example utilizing these APIs. This will enhance your understanding and application of UDB in your projects.

Getting Started with UDB

  
  // Install UDB
  npm install udb

  // Import UDB
  const udb = require('udb');
  

Connecting to a Database

  
  // Define database connection settings
  const config = {
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'testdb',
  };

  // Create a connection
  const db = new udb.Connection(config);

  // Connect to the database
  db.connect().then(() => {
    console.log('Connected to the database');
  }).catch(err => {
    console.log('Connection error:', err);
  });
  

Creating a Table

  
  const createTableQuery = `
    CREATE TABLE users (
      id INT AUTO_INCREMENT,
      name VARCHAR(100),
      email VARCHAR(100),
      PRIMARY KEY (id)
    )
  `;

  db.query(createTableQuery).then(() => {
    console.log('Table created');
  }).catch(err => {
    console.log('Error creating table:', err);
  });
  

Inserting Data

  
  const insertQuery = 'INSERT INTO users (name, email) VALUES (?, ?)';
  const values = ['John Doe', 'john@example.com'];

  db.query(insertQuery, values).then(result => {
    console.log('Data inserted');
  }).catch(err => {
    console.log('Error inserting data:', err);
  });
  

Fetching Data

  
  const selectQuery = 'SELECT * FROM users';

  db.query(selectQuery).then(rows => {
    console.log('Data fetched', rows);
  }).catch(err => {
    console.log('Error fetching data:', err);
  });
  

Updating Data

  
  const updateQuery = 'UPDATE users SET email = ? WHERE id = ?';
  const values = ['newemail@example.com', 1];

  db.query(updateQuery, values).then(result => {
    console.log('Data updated');
  }).catch(err => {
    console.log('Error updating data:', err);
  });
  

Deleting Data

  
  const deleteQuery = 'DELETE FROM users WHERE id = ?';
  const userId = 1;

  db.query(deleteQuery, [userId]).then(result => {
    console.log('Data deleted');
  }).catch(err => {
    console.log('Error deleting data:', err);
  });
  

Complete App Example

  
  const udb = require('udb');

  const config = {
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'testdb',
  };

  const db = new udb.Connection(config);

  async function runApp() {
    try {
      await db.connect();

      const createTableQuery = `
        CREATE TABLE IF NOT EXISTS users (
          id INT AUTO_INCREMENT,
          name VARCHAR(100),
          email VARCHAR(100),
          PRIMARY KEY (id)
        )
      `;
      await db.query(createTableQuery);

      const insertQuery = 'INSERT INTO users (name, email) VALUES (?, ?)';
      const values = ['Jane Doe', 'jane@example.com'];
      await db.query(insertQuery, values);

      const selectQuery = 'SELECT * FROM users';
      const users = await db.query(selectQuery);
      console.log(users);

      const updateQuery = 'UPDATE users SET email = ? WHERE id = ?';
      const updateValues = ['jane_updated@example.com', 1];
      await db.query(updateQuery, updateValues);

      const deleteQuery = 'DELETE FROM users WHERE id = ?';
      await db.query(deleteQuery, [1]);

      await db.disconnect();
      console.log('App finished successfully');
    } catch (err) {
      console.error('An error occurred:', err);
    }
  }

  runApp();
  

Using UDB, developers can simplify the management of database connections and operations, making development more efficient and streamlined. Leveraging the powerful features of UDB as demonstrated above will significantly enhance your database interactions.

Hash: 3261c7c02e459177b631d6d9ceb42e5df8426f7d356b9174db653d1be0d19166

Leave a Reply

Your email address will not be published. Required fields are marked *