Home >Web Front-end >JS Tutorial >Detailed explanation of examples of using Sqlite database in React Native
The command line enters the root directory of the ReactNative project and executes
##npm install React-native-sqlite-storage --save
Add compile project(':react-native-sqlite-storage')
4. Edit the MainApplication.Java file, Register the sqlite module in MainActivitiy.java
##import org.pgsqlite.SQLitePluginPackage;##protected List
##return Arrays.< ;ReactPackage>asList(
## ,
Screenshot below:
5. Use
Pay attention to each printing exception, otherwise you may not see what went wrong
[javascript] view plain copy
import React,{Component} from 'react';
import{
'react-native';
import SQLiteStorage from 'react-native-sqlite-storage';
//Database file
//Version number
##var database_size = -1;
##var db;
export
default## componentWillUnmount(){
this._successCB('close');
db.close();
## }## }
}
return db; ## }
createTable(){
if (!db) {
this.open();
tx.executeSql(
'CREATE TABLE IF NOT EXISTS USER(' +# , , , ##
this._successCB(##
this._errorCB('executeSql', err);
}, (err)=> {'transaction', err);
'transaction');
deleteData(){
if (!db) {
##this.open();db.transaction((tx)=>{
tx.executeSql(}
dropTable(){
## db.transaction((tx)=>{});
## },()=>{
this._successCB(
insertUserData(userData){
## }
this.createTable();
this.deleteData();
## let sex = user.sex;
let phone = user.phone;
let email = user.email;
let qq = user.qq;
let sql = "INSERT INTO user(name,age,sex,phone,email,qq)"+
"values(?,?,?,?,?,?)";
tx.executeSql(sql,[name,age,sex,phone,email,qq],()=>{
},(err)=>{
console.log(err);
}
);
}
},(error)=>{
this._errorCB('transaction', error);
ToastAndroid.show("数据插入失败",ToastAndroid.SHORT);
},()=>{
this._successCB('transaction insert data');
ToastAndroid.show("成功插入 "+len+" 条用户数据",ToastAndroid.SHORT);
});
}
close(){
if(db){
this._successCB('close');
db.close();
}else {
console.log("SQLiteStorage not open");
}
db = null;
}
_successCB(name){
console.log("SQLiteStorage "+name+" success");
}
_errorCB(name, err){
console.log("SQLiteStorage "+name);
console.log(err);
}
render(){
return null;
}
};
'react';
##import {
'./sqlite';
new SQLite();
## compennenetDidUnmount(){
sqLite.close( );
}
componentWillMount(){
//Open the database
## if(!db){
db = sqLite.open();
//Create table
sqLite.createTable();
//Delete data
## sqLite.deleteData();
var userData = [];
##var user = {};
"张三";
## user.phone =
user.email =
user.qq =
## userData.push(user);
//Insert data
//Query
##
alert(
", Phone: "+u.phone);
} }
},(error)=>{
//Print exception information## Console.log(error);
});
}
render(){
return null;
}
}
The above is the detailed content of Detailed explanation of examples of using Sqlite database in React Native. For more information, please follow other related articles on the PHP Chinese website!