- Notifications
You must be signed in to change notification settings - Fork8
A react native android wrapper for SQLite
License
jbrodriguez/react-native-android-sqlite
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A react native android wrapper for SQLite
React Native doesn't have a built-in module to access Sqlite databases, either in iOS or Android.
This library intends to fill the gap on the Android side.
- Install Module
npm install --save-dev react-native-android-sqlite
android/settings.gradle
...include':react-native-android-sqlite'project(':react-native-android-sqlite').projectDir=newFile(settingsDir,'../node_modules/react-native-android-sqlite')
android/app/build.gradle
dependencies {...compile project(':react-native-android-sqlite')}
- register module (in MainActivity.java)
...importio.jbrodriguez.react.*;// <--- importpublicclassMainActivityextendsActivityimplementsDefaultHardwareBackBtnHandler {...@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);mReactRootView =newReactRootView(this);mReactInstanceManager =ReactInstanceManager.builder() .setApplication(getApplication()) .setBundleAssetName("index.android.bundle") .setJSMainModuleName("index.android") .addPackage(newMainReactPackage()) .addPackage(newRNSQLiteModule())// <- add here .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .build();mReactRootView.startReactApplication(mReactInstanceManager,"YourProject",null);setContentView(mReactRootView); }}
This library depends onSQLiteAssetHelper.
The idea is that youimport
your previously created database as an application asset.
SQLiteAssetHelper manages schema definition (create), as well as upgrades.
For more information refer toSQLiteAssetHelper's docs and/or checkSQLiteOpenHelper
So, the first step involves copying your sqlite db to the following folder
<YourProject>/android/app/src/main/assets/databases
Substitute<YourProject>
with the folder where your app resides, i.e. AwesomeProject.
NOTE: Originally, I suggested copying the sqlite db to
<ReactNativeRootFolder>/node_modules/react-native-android-sqlite/src/main/assets/databases
. Although it works, the db will be erased each time you upgrade this component, so it's better to follow the updated instructions.
Having done that, you can start interacting with the db, through 4 public functions:
- init
- query
- exec
- close
The database must be initialized before any other call takes place
varsqlite=require('react-native-android-sqlite')vardatabaseName='app.db'sqlite.init(databaseName).then((_)=>{console.log('database initialized.')})
pre-requisite: the db must have been initialized
varsqlite=require('react-native-android-sqlite')varsql='INSERT INTO todo(name, completed) VALUES (?, ?)'varparams=["Create react native android sqlite",1]sqlite.exec(sql,params).then((_)=>{console.log('row inserted.')})
pre-requisite: the db must have been initialized
varsqlite=require('react-native-android-sqlite')varsql='SELECT * FROM todo WHERE completed = ?'varparams=[1]sqlite.query(sql,params).then((data)=>{console.log('retrieved: ',data)})
pre-requisite: the db must have been initialized
sqlite.close().then((_)=>{console.log('database closed')})
- It doesn't return the id for a newly inserted row (maybe create a separate insert function ?)
- Column types currently supported are Integer and String
- Additional error handling should be implemented
- Although I'm using it in my personal projects, it's still an early release. Please do read thelicense
Please submit any PR's you seem fit.
- React Native - Awesome software.
- Android SQLiteAssetHelper - Simplifying the handling of Android's sqlite interface
- React Native Android Badge - For showing me the light with regards to the gradle build system, as applied to react native
MIT
About
A react native android wrapper for SQLite
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.