realm数据库是一款小型数据库系统,可以支持多个平台,如android、ios、javascript等。当然了realm也是可以支持react-native的,官网有具体介绍,官网文档

安装realm

npm install --save realm

然后link

react-native link realm

或者

rnpm link realm

如果link不完全,可以手动检查添加

1.Add the following lines to android/settings.gradle:

include ':realm'
project(':realm').projectDir =new File(rootProject.projectDir,'../node_modules/realm/android')

2.Add the compile line to the dependencies in android/app/build.gradle:

dependencies {compile project(':realm')
}

3.Add the import and link the package in MainApplication.java:

import io.realm.react.RealmReactPackage; // add this importpublic class MainApplication extends Application implements ReactApplication {@Overrideprotected List<ReactPackage> getPackages() {return Arrays.<ReactPackage>asList(new MainReactPackage(),new RealmReactPackage() // add this line);}
}

React-native中用到的realm是一款对象数据库,因此使用起来相关简单方便;
然后关于其操作:
1. 打开数据库的方式,按照其介绍有两种方式
其一、通过 open方法,eg:

// Define your models and their properties
const CarSchema = {name: 'Car',properties: {make:  'string',model: 'string',miles: {type: 'int', default: 0},}
};
const PersonSchema = {name: 'Person',properties: {name:     'string',birthday: 'date',cars:     'Car[]',picture:  'data?' // optional property}
};Realm.open({schema: [CarSchema, PersonSchema]}).then(realm => {// Create Realm objects and write to local storagerealm.write(() => {const myCar = realm.create('Car', {make: 'Honda',model: 'Civic',miles: 1000,});myCar.miles += 20; // Update a property value});// Query Realm for all cars with a high mileageconst cars = realm.objects('Car').filtered('miles > 1000');// Will return a Results object with our 1 carcars.length // => 1// Add another carrealm.write(() => {const myCar = realm.create('Car', {make: 'Ford',model: 'Focus',miles: 2000,});});// Query results are updated in realtimecars.length // => 2});

其二、通过new产生一个对象

const PersonSchema = {name: 'Person',properties: {name: 'string',testScores: 'double?[]'}
};let realm = new Realm({schema: [PersonSchema, CarSchema]});realm.write(() => {let charlie = realm.create('Person', {name: 'Charlie',testScores: [100.0]});// Charlie had an excused absense for the second test and was allowed to skip itcharlie.testScores.push(null);// And then he didn't do so well on the third testcharlie.testScores.push(70.0);
});
  1. 插入数据
let realm = new Realm({schema: [PersonSchema, CarSchema]});
realm.write(() => {let charlie = realm.create('Person', {name: 'Charlie',testScores: [100.0]});
});
  1. 查询数据

假设有表 Dog,下面获取Dog表所有数据。

let dogs = realm.objects('Dog'); // retrieves all Dogs from the Realm

有时候我们需要做数据筛选,可以这样写

query(name){let dogs = realm.objects('Dog');let tanDogs = dogs.filtered('color = "tan" AND name "'+name+'"');
}

具体的筛选条件可以用到下面这些

At the moment only a subset of the NSPredicate syntax is supported in the query language. Basic comparison operators ==, !=, >, >=, <, and <= are supported for numeric properties. ==, BEGINSWITH, ENDSWITH, and CONTAINS are supported for string properties. String comparisons can be made case insensitive by appending [c] to the operator: ==[c], BEGINSWITH[c] etc. Filtering by properties on linked or child objects can by done by specifying a keypath in the query eg car.color == 'blue'

有时候我们还需要排序

let hondas = realm.objects('Car').filtered('make = "Honda"');// Sort Hondas by mileage
let sortedHondas = hondas.sorted('miles');// Sort in descending order instead
sortedHondas = hondas.sorted('miles', true);// Sort by price in descending order and then miles in ascending
sortedHondas = hondas.sorted(['price', true], ['miles']);

results也可以通过存储对象的链接对象进行排序,如:

let people = realm.objects('Person');// Sort people by the milage of their cars
let sortedPeople = people.sorted('car.miles');

未完待续..

react-native之Realm数据库的使用(一)相关推荐

  1. 使用GraphQL,React Native和AWS AppSync编写应用程序代码:该应用程序

    您将要创造的 在这些教程中,我将向您展示如何使用AWS AppSync和React Native创建GraphQL数据库并与之交互. 该应用程序将具有实时和脱机功能,我们通过AppSync开箱即用. ...

  2. 使用GraphQL,React Native和AWS AppSync编写应用程序代码:后端

    您将要创造的 在这些教程中,我将向您展示如何使用AWS AppSync和React Native创建GraphQL数据库并与之交互. 该应用程序将具有实时和脱机功能,我们通过AppSync开箱即用. ...

  3. 我在React Native中构建时获得的经验教训

    by Amanda Bullington 通过阿曼达·布林顿(Amanda Bullington) 我在React Native中构建时获得的经验教训 (Lessons I learned while ...

  4. 从零学React Native之13 持久化存储

    数据持久化就是指应用程序将某些数据存储在手机存储空间中. 借助native存储 这种方式不言而喻,就是把内容传递给native层,通过原生API存储,详见从零学React Native之05混合开发 ...

  5. 移动开发者如何更好地学习 React Native? | 技术头条

    作者 | 魔笛 责编 | 郭芮 2015年3月,Facebook正式发布react-native,只支持iOS平台:2015年9月,Facebook发布了React Native for Androi ...

  6. React Native专题

    未经授权不得转载: 出处地址:http://www.lcode.org 本文出自:[江清清的技术专栏] 本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入 ...

  7. React Native专题-江清清

    本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶相关讲解. 刚创建的React Native交流8群:533435865  欢迎各位大牛, ...

  8. React Native专题-江

    (一).基本介绍: 江博客http://blog.csdn.net/jiangqq781931404/article/category/6055594 React Native For Android ...

  9. React Native开发(一)

    本React Native讲解专题:主要讲解了React Native开发,由基础环境搭建配置入门,基础,进阶相关讲解. 关于React Native各种疑难杂症,问题深坑总结方案请点击查看: Mac ...

  10. React Native开发速记

    文章目录 引子 React Native适用场景 React基础 JSX 组件的定义 高阶组件(HOC) 生命周期函数与组件渲染 组件的缺省属性 组件之间的通信 基础API Flex弹性布局 例子: ...

最新文章

  1. JQuery 1.32 DatePicker 增强版
  2. 基建狂魔:硬核技术之隔绝厌氧菌的涂料
  3. 人工智能赋能智慧停车 准确预订车位
  4. 解决会声会影x7 x8打开即“已停止工作问题”
  5. python编写程序的一般步骤-Python:开发_基本流程
  6. 《大数据分析原理与实践》——习题
  7. vue使用html渲染组件,Vue.js在渲染组件之前填充数据
  8. 什么是HBase?它是怎样工作的?终于有人讲明白了
  9. C++STL笔记(十):queue详解
  10. flex 平铺布局_Flex布局的个人见解~阮一峰的网络日志
  11. python selenium 等待页面加载完毕_python3 selenium 设置元素等待的三种方法
  12. 中海达数据怎么转rinex_GPS_OEM原始数据向Rinex格式转换的方法[1]
  13. 14AMESIM安装教程
  14. TouchDesigner学习 全屏输出
  15. Android color颜色-色号总结
  16. java main 参数解析_Java Main参数解析(Args4j)
  17. 远程桌面计算机密码是多少,局域网远程桌面连接密码
  18. 网络诊断提示:远程计算机或设备将不接受连接 (根治 )
  19. 笔记本禁用键盘的方法(已试过win10/win11均可生效)
  20. 夏普电视显示服务器忙碌或网络异常,夏普电视故障常见有哪些?

热门文章

  1. 携程千人规模团队研发效能提升实践
  2. slab、slob和slub
  3. c++中计算2得n次方_生物高考常考计算题型归纳与答题公式
  4. Laya Air - 如何在Laya3D中实现屏幕后期特效?
  5. 韩语计算机术语大全,韩语词汇辅导:韩语计算机、互联网术语1
  6. mysql查询replace用法详解
  7. KF2在windows开服教程
  8. 显示器连接服务器鼠标没有反应怎么回事,解决台式机开机后显示器键盘鼠标都没反应的方法...
  9. AA-PEG-AA,Acetic Acid-PEG-Acetic Acid,羧酸-聚乙二醇-羧酸供应
  10. C++ 打怪游戏 原创 小镇5.0正式版--怪物狂欢季