npm official website guide: https://www.npmjs.com/package/react-native-sqlite-storage
1. 执行: npm install react-native-sqlite-storage
2.cd ios execution pod install
3. Perform react native link
use:
According to the official website said above, in the root directory of the new project ios www folder, which put sqlite database file
Use react native project:
1. 导入 import SQLiteManager from 'react-native-sqlite-storage'
2. Open the database:
var DB = SQLiteManager.openDatabase ({name: "mydata.db,", createFromLocation:}. 1, openCB, errorCB); // parameters are: the database name, path, successful operation function, operation function fails
function openCB() { console.log('open!') } function errorCB(err) { console.log(err) }
/ * * * [Closedb Close Database] * / function closedb () { IF (DB) { db.Close () } the else { } }
3. Create a table:
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. Query
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 package CRUD Method:
https://www.jianshu.com/p/69a2e7e93caf
Encapsulated tool: https: //linux.ctolib.com/NikiLee2016-react-native-sqlite-helper-pro.html