Use Sqlite in react-native

First of all install sqlite using below command

npm i react-native-sqlite-storage

import React, { useEffect } from "react";
import { openDatabase } from "react-native-sqlite-storage";
var db = openDatabase({ name: 'my.db' });
function App(){
    useEffect(() => {
        db.transaction(function (txn) {
            txn.executeSql(
                "SELECT name FROM sqlite_master WHERE type='table' AND name='price_data'",
                [],
                function (tx, res) {
                    console.log('item:', res.rows.length);
                    if (res.rows.length == 0) {
                        txn.executeSql('DROP TABLE IF EXISTS price_data', []);
                        txn.executeSql(
                            'CREATE TABLE IF NOT EXISTS price_data(id INTEGER PRIMARY KEY AUTOINCREMENT, item_name VARCHAR(20), price INT(10))',
                            []
                        );
                    }
                }
            );
        });
     
   
    }, []);
    //   // insert data
    //   db.transaction(function (tx) {
    //     tx.executeSql(
    //       'INSERT INTO price_data (item_name, price) VALUES (?,?)',
    //       ['Neeraj','20'],

    //       (tx, results) => {
    //         console.log('Results', results.rowsAffected);
    //         if (results.rowsAffected > 0) {
    //          console.log('success');
    //         } else {
    //             console.log("failed")
    //         }
    //       }
    //     );
    //   });
}
export default App

For more detials click Sqlite

Comments