SQLite3 Native Plugin for React Native
0 0
Read Time:2 Minute, 23 Second

react-native-sqlite-2

SQLite3 Native Plugin for React Native for Android, iOS, Windows and macOS. This plugin provides a WebSQL-compatible API to store data in a react native app, by using a SQLite database on the native side.

Inspired by fantastic work done by Nolan Lawson. It should be a drop-in replacement for react-native-sqlite-storage. It works pretty well with PouchDB on React Native app.

 

Get Started By Installing react-native-sqlite-2

Add react-native-sqlite-2 to your dependencies:

If You Are Using NPM : 

$ npm install react-native-sqlite-2 --save

OR

If you Are Using YARN:

$ yarn install react-native-sqlite-2

Linking Dependencies:

If you are using react-native <0.60 then you will require to manually Link dependencies.

From react-native 0.60 auto linking will take care of the link step but don’t forget to run $ pod install

from ios Directory.

$ react-native link react-native-sqlite-2

For Mac/ios Users:

If using cocoapods:

$ cd ios
$ pod install

For Android Users:

For Android, Please Make sure that AndroidX is Enabled in your android/gradle.properties

 

android.useAndroidX=true 
android.enableJetifier=true

 

Demo Usage:

here is a sample code:

import SQLite from "react-native-sqlite-2";

const db = SQLite.openDatabase("test.db", "1.0", "", 1);
db.transaction(function (txn) {
  txn.executeSql("DROP TABLE IF EXISTS Users", []);
  txn.executeSql(
    "CREATE TABLE IF NOT EXISTS Users(user_id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(30))",
    []
  );
  txn.executeSql("INSERT INTO Users (name) VALUES (:name)", ["nora"]);
  txn.executeSql("INSERT INTO Users (name) VALUES (:name)", ["takuya"]);
  txn.executeSql("SELECT * FROM `users`", [], function (tx, res) {
    for (let i = 0; i < res.rows.length; ++i) {
      console.log("item:", res.rows.item(i));
    }
  });
});

Using with PouchDB:

It can be used with pouchdb-adapter-react-native-sqlite. Here is a Sample Code:

import PouchDB from "pouchdb-react-native";
import SQLite from "react-native-sqlite-2";
import SQLiteAdapterFactory from "pouchdb-adapter-react-native-sqlite";

const SQLiteAdapter = SQLiteAdapterFactory(SQLite);
PouchDB.plugin(SQLiteAdapter);
var db = new PouchDB("mydb", { adapter: "react-native-sqlite" });

TroubleShoot Guide:

Row too big to fit into CursorWindow (Android). You can set a limited windowSizeBytes for CursorWindow and try-catch the exception by adding following code to your MainApplication.onCreate in MainApplication.java :

Note: It requires Android 9 (API level 28).
try {
  Field field = CursorWindow.class.getDeclaredField("sCursorWindowSize");
  field.setAccessible(true);
  field.set(null, 100 * 1024 * 1024); //the 100MB is the new size
} catch (Exception e) {
  if (DEBUG_MODE) {
    e.printStackTrace();
  }
}

SQLite3 Native Plugin for React Native.zip

FREE DOWNLOAD

Send download link to:

If You Have Any Problem Or Doubts,
Please Do Ask Or Suggest In Comment.


Do Follow Us on:

Facebook.com

Twitter.com

Also Checkout: Instagram Like Zoomable Image Using Native Animation

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

By Akash Kothari

I am passionate about my work. Because I love what I do, I have a steady source of motivation that drives me to do my best. In my last job, this passion led me to challenge myself daily and learn new skills that helped me to do better work

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

2 thoughts on “SQLite3 Native Plugin for React Native

Leave a Reply

Discover more from TechDevil

Subscribe now to keep reading and get access to the full archive.

Continue reading