DEV Community

Deepak Sen (Web Developer)
Deepak Sen (Web Developer)

Posted on

SQLite Database & Expo SQLite A much easy to use docs.

Introduction to SQLite Database

An sqlite database is c lang. lib.implements Small Fast+Self contained+High reliability. Sql Db engine
It is most uses database in World
SQLite Built into the Mobiles,computer devices bundled inside
countless other application people uses every day.

The Latest Version of this Version 3.53.0
check version
sqlite3 --version

Uses as A:-

the primary engine for high-performance, persistent local data storage
It is essential for applications that require complex data relationships, offline-first functionality, or the management of large datasets that exceed the capabilities of simple key-value stores like AsyncStorage

What are the roles of sqlite in The react native or applications ?

  • Offline-First Data Management
  • Relational Data integrity
  • High-Performance Querying
  • API Caching

Popular SQLite Libraries for React Native
Choosing the right library depends on your environment (Expo vs. Bare React Native):

  • Expo SQLite: The standard for Expo projects. It provides a modern API with support for React Hooks, SQLiteProvider, and React Suspense for smooth data loading.

React native SQLite is best for both devices iOS and Android

  • react-native-sqlite-storage: A long-standing, battle-tested library for "bare" React Native projects, supporting both Android and iOS with an identical API.

  • Nitro SQLite (formerly Quick-SQLite): A high-performance alternative using JSI (JavaScript Interface) to communicate directly with C++, bypassing the standard React Native Bridge for significantly faster operations.

  • op-sqlite: Focuses on speed and low-level control, allowing for custom compilation flags and better performance for massive datasets.

Introduction
Expo SQlite is best for easily use sql offline store functionality in Expo React Native

What are the functions in Expo SQlite
openDatabaseAsync

  • where we define our database
  • import * as SQLite from 'expo-sqlite'
  • make a instance in db variable We need this function for init our db

execAsync

  • where we execute the initials and Queries to our database
  • parameter is that Query SQL

getAllAsync

  • this function for SELECT Query
  • it returns the SQL [] array for as a Data

runAsync

  • It we can manipulate datas Interting data

Step1
Initial Code

import * as SQLite from 'expo-sqlite';

const db = SQLite.openDatabaseSync('products.db');

export const initDB = async () => {
  await db.execAsync(`
    CREATE TABLE IF NOT EXISTS products (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT,
      price REAL
    );
  `);
};

export default db;

Enter fullscreen mode Exit fullscreen mode

step 2

const [products, setProducts] = useState([]);

const getAllproducts=async()=>{
 const result = await db.getAllAsync("SELECT * FROM products");
 setProducts(result);
}
const addProduct =async(name,price)=>{  
    if(!name || !price){
        Alert.alert("Please enter all the fields");
        return;
    }
    await db.runAsync("INSERT INTO products (name, price) VALUES (?, ?)", [name, price]);
    Alert.alert('Success','Product added successfully')
    getAllproducts();
}

const deleteProduct =async(id)=>{
    await db.runAsync("DELETE FROM products WHERE id = ?", [id]);
    getAllproducts();
}

const editProduct =async(id,name,price)=>{
    await db.runAsync("UPDATE products SET name = ?, price = ? WHERE id = ?", [name, price, id]);
    getAllproducts();
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)