要安装react-native-sqlite-storage包,你可以按照以下步骤进行操作:
打开终端,导航到你的React Native项目的根目录。
运行以下命令来安装react-native-sqlite-storage包:
npm install react-native-sqlite-storage --save
react-native link react-native-sqlite-storage
cd ios
pod install
cd ..
react-native run-ios
import SQLite from 'react-native-sqlite-storage';
// 创建或打开数据库
const db = SQLite.openDatabase(
{
name: 'database.db',
location: 'default',
createFromLocation: '~www/database.db', // 如果想从预置数据库文件创建,则需要指定此选项
},
() => {
// 数据库创建/打开成功回调
console.log('数据库打开成功');
},
error => {
// 数据库创建/打开失败回调
console.error('数据库打开失败', error);
}
);
// 执行SQL查询
db.transaction(transaction => {
transaction.executeSql(
'SELECT * FROM table_name',
[],
(transaction, resultSet) => {
// 查询成功回调
console.log('查询结果:', resultSet);
},
(transaction, error) => {
// 查询失败回调
console.error('查询失败:', error);
}
);
});
这样,你就可以使用react-native-sqlite-storage包在你的React Native项目中进行SQLite数据库操作了。
上一篇:安装 react-native-paper 后配置出错
下一篇:安装 react-styleguidist,创建 React 应用程序,使用 Typescript,Material UI 和 styled-components。