一、环境配置
1、创建一个ReactNative项目(RealmDemo)
react-native init RealmDemo
2、切换目录
cd RealmDemo
3、添加realm依赖库
npm install --save realm
4、关联realm库
rnpm link realm
注意:
如果关联失败,出现如下错误:
rnpm-link info Linking realm android dependency
rnpm-link info Android module realm has been successfully linked
rnpm-link info Linking realm ios dependency
rnpm-link info iOS module realm has been successfully linked
rnpm-link ERR! It seems something went wrong while linking. Error: spawn UNKNOWN
Please file an issue here: https://github.com/rnpm/rnpm/issues
则需要在 MainApplication 中添加
new RealmReactPackage()
不然会在运行时报错:
android Missing Realm constructor - please ensure RealmReact framework is included
还需要注意的是,添加过程中有可能出现很多奇怪的现象,有可能链接不上,也可能链接上了没有自动添加代码,需要手动处理:
settings.gradle中是否存在下面的代码,如不存在手动添加
include ':realm'
project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
app->build.gradle中是否存在如下代码
dependencies {
compile project(':realm')//是否存在,不存在手动添加
compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1"
}
5、运行项目即可
react-native run-android
目前为止,Realm库就算配置成功了
二、Realm具体操作
作为一个数据库,最基本的增删改查是必须的,
1、建表
primaryKey属性,该属性的类型可以为string和int类型。声明主键之后,在更新和设置值的时候该值必须保持唯一。一旦一个使用主键的对象被添加到Realm之后,该主键值就无法被修改啦。
const CarSchema = {
name: 'Car',
primaryKey: 'id',
properties: {
id: 'int',
car_type: 'string',
car_name: 'string',
driver_name: 'string',
phone: {type: 'string', default: '10086'},//添加默认值的写法
}
};
//初始化Realm
let realm = new Realm({schema: [CarSchema]});
2、写数据
2.1 增
realm.write(()=> {
realm.create('Car', {id: 1, car_type: 'QQ', car_name: 'SB001', driver_name: '张三'});
realm.create('Car', {id: 2, car_type: '宝马', car_name: 'SB002', driver_name: '李四'});
realm.create('Car', {id: 3, car_type: '奔驰', car_name: 'SB003', driver_name: '王五'});
})
2.2 删
realm.write(()=> {
let cars = realm.objects('Car');
let car = cars.filtered('id==3');
realm.delete(car);
});
2.3 改
//更新id = 1的数据
realm.write(()=> {
realm.create('Car', {id: 2, driver_name: 'feiyang'}, true)
ToastAndroid.show("修改完成...", ToastAndroid.SHORT);
});
2.4 查
根据id=2 进行查询数据
let cars = realm.objects('Car');
let car = cars.filtered('id==2');
if (car) {
ToastAndroid.show('Car的数据为,'
+ '编号=' + car[0].id
+ 'car_type=' + car[0].car_type
+ 'car_name=' + car[0].car_name
+ 'driver_name=' + car[0].driver_name, ToastAndroid.SHORT);
}
查询所有
let cars = realm.objects('Car');
ToastAndroid.show('Car的数据为,'+cars.length, ToastAndroid.SHORT);
下边是完整代码
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, {Component} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
TouchableHighlight,
ToastAndroid,
} from 'react-native';
const Realm = require('realm');
class CustomButton extends Component {
render() {
return (
<TouchableHighlight
style={styles.button}
underlayColor="#a5a5a5"
onPress={this.props.onPress}>
<Text style={styles.buttonText}>{this.props.text}</Text>
</TouchableHighlight>
);
}
}
class Test extends Component {
render() {
const CarSchema = {
name: 'Car',
primaryKey: 'id',
properties: {
id: 'int',
car_type: 'string',
car_name: 'string',
driver_name: 'string',
}
};
//初始化Realm
let realm = new Realm({schema: [CarSchema]});
return (
<View style={{marginTop: 20}}>
<Text style={styles.welcome}>
Realm基础使用实例-增删改查
</Text>
<CustomButton
text="表新增"
onPress={()=>
realm.write(()=> {
realm.create('Car', {id: 1, car_type: 'QQ', car_name: 'SB001', driver_name: '张三'});
realm.create('Car', {id: 2, car_type: '宝马', car_name: 'SB002', driver_name: '李四'});
realm.create('Car', {id: 3, car_type: '奔驰', car_name: 'SB003', driver_name: '王五'});
realm.create('Car', {id: 4, car_type: '劳斯莱斯', car_name: 'SB004', driver_name: '张六'});
realm.create('Car', {id: 5, car_type: '比亚迪', car_name: 'SB005', driver_name: '理七'});
ToastAndroid.show('添加数据完成', ToastAndroid.SHORT);
})
}/>
<CustomButton
text="表修改"
onPress={()=> {
//更新id = 1的数据
realm.write(()=> {
realm.create('Car', {id: 2, driver_name: 'feiyang'}, true)
ToastAndroid.show("修改完成...", ToastAndroid.SHORT);
});
}}
/>
<CustomButton
text="表数据删除-删除id=3的数据"
onPress={()=> {
realm.write(()=> {
let cars = realm.objects('Car');
let car = cars.filtered('id==3');
realm.delete(car);
});
}}
/>
<CustomButton
text="查询所有数据"
onPress={()=> {
let cars = realm.objects('Car');
ToastAndroid.show('Car的数据为,'+cars.length, ToastAndroid.SHORT);
}}
/>
<CustomButton
text="根据id=2 进行查询数据"
onPress={()=> {
let cars = realm.objects('Car');
let car = cars.filtered('id==2');
if (car) {
ToastAndroid.show('Car的数据为,'
+ '编号=' + car[0].id
+ 'car_type=' + car[0].car_type
+ 'car_name=' + car[0].car_name
+ 'driver_name=' + car[0].driver_name, ToastAndroid.SHORT);
}
}}
/>
</View>
)
}
}
const styles = StyleSheet.create({
welcom: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
button: {
margin: 3,
backgroundColor: 'white',
padding: 10,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#cdcdcd'
},
});
AppRegistry.registerComponent('RealmDemo', () => Test);
三、总结
本文主要是Realm数据库基本的增删改查。
在此感谢http://www.lcode.org/react-native/
git地址:
https://git.oschina.net/feiyangwei/ReactNativeRealmDemo.git