|
npm 官网指导: https://www.npmjs.com/package/react-native-sqlite-storage
1. 执行: npm install react-native-sqlite-storage
2.cd ios 执行pod install
3.执行react native link
使用:
按照上面官网说的,在ios项目根目录下新建www文件夹,里面放sqlite数据库文件
在react native项目中使用:
1. 导入 import SQLiteManager from 'react-native-sqlite-storage'
2.打开数据库:
var db = SQLiteManager.openDatabase({name: "mydata.db", createFromLocation: 1}, openCB, errorCB); //参数依次是:数据库名,路径,操作成功函数,操作失败函数
function openCB() {
console.log('open!')
}
function errorCB(err) {
console.log(err)
}
/**
* [closeDB 关闭数据库]
*/
function closeDB(){
if(db){
db.close()
}else {
}
}
3.创建表:
db.transaction((tx) => {
//创建表
tx.executeSql('CREATE TABLE IF NOT EXISTS DATA(' +
'id INTEGER PRIMARY KEY AUTOINCREMENT,' +
'title VARCHAR,'+
'value VARCHAR,' +
'time VARCHAR,' +
'year VARCHAR,' +
'month VARCHAR,' +
'date VARCHAR)',[],() => {Alert.alert('createTable executeSql success')},
(err) => { Alert.alert('createTable executeSql error=',err)})
}
)
4.查询
db.transaction((tx) => {
tx.executeSql(select, [], (tx, result) => {
let arr = []
for (let i = 0; i < result.rows.length; i++) {
arr.push(result.rows.item(i))
}
this.setState({
dataSource: arr
})
})
});//select 是sql语句
RN- react-native-sqlite-storage 封装增删改查方法:
https://www.jianshu.com/p/69a2e7e93caf
封装好的工具:https://linux.ctolib.com/NikiLee2016-react-native-sqlite-helper-pro.html
react-native-sqlite-helper-pro
此文仅为鄙人学习笔记之用,朋友你来了,如有不明白或者建议又或者想给我指点一二,请私信我。liuw_flexi@163.com/QQ群:582039935.
我的gitHub: (学习代码都在gitHub)
https://github.com/nwgdegitHub/
来源:https://www.cnblogs.com/liuw-flexi/p/11534893.html |