0

我正在尝试通过在一个简单的应用程序上将 sqlite 与 react native 链接来开始.. 但不知何故一直低于错误:我尝试重新安装、重新链接、删除模块和纱线启动......

请帮忙!未找到模块:即使 sqlite.core.js 文件位于 ./node_modules/react-native-sqlite-storage/lib/sqlite.core.js 中,也无法解析“react-native”

错误: “./node_modules/react-native-sqlite-storage/lib/sqlite.core.js 未找到模块:无法解析 'C:\Users\accd8\Documents\1 Expertise\React 中的 'react-native' \TechTim\my-app\node_modules\react-native-sqlite-storage\lib'"

以下是在浏览器上: 浏览器错误消息

图像显示库 node_modules/react-native-sqlite-storage/lib/sqlite.core.js

我的包裹杰森:

####{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-native-sqlite-storage": "^5.0.0",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
####

APP.js

###import React from 'react'
import { useEffect } from 'react';
import { View, Text, Button } from 'react-native'
import SQLite from 'react-native-sqlite-storage'
const db = SQLite.openDatabase({
  location: "default",
  name: "SqliteDb"
}, () => {
  console.log('başarılı')
}, (err) => {
  console.log('hata')
})
const App = () => {

  useEffect(() => {
    db.transaction((tx) => {
      tx.executeSql("CREATE TABLE IF NOT EXISTS students (ID INTEGER PRIMARY KEY AUTOINCREMENT , Name TEXT, AGE INTEGER)", [], (tx, result) => {
        console.log('tx', tx)
        console.log('result', result)
      })
    })
  }, [])
  const createRecord = () => {
    for (let index = 0; index < 20; index++) {
      const name = "Öğrenci" + index
      const age = Math.floor(Math.random() * 50)
      db.transaction((tx) => {
        tx.executeSql('INSERT INTO students (Name,Age) VALUES(?,?)', [name, age], (tx, result) => {
          console.log('tx', tx)
          console.log('result', result)

        })
      })
    }
  }
  const readRecord = () => {
    db.transaction((tx) => {
      tx.executeSql('SELECT * FROM students', [], (tx, result) => {
        console.log('result', result)
        for (let index = 0; index < result.rows.length; index++) {
          console.log(result.rows.item(index))

        }
      })
    })
  }
  const deleteRecord = () => {
    db.transaction((tx) => {
      tx.executeSql('DELETE FROM students where id = ? ', [1], (tx, result) => {
        console.log(`tx`, tx)
        console.log(`result`, result)

      })
    })
  }
  return (
    <View>
      <Button title="Ekle" onPress={createRecord} />
      <Button title="Sil" onPress={deleteRecord} />
      <Button title="Oku" onPress={readRecord} />

      <Text>
        React Native sqlite
      </Text>
    </View>
  )
}
export default App


/*import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Learn React
        </a>
      </header>
    </div>
  );
}

export default App;
*/
4

1 回答 1

0

您使用的是React,而不是React Native,这是两个不同的东西。React 用于 Web,React Native 用于移动。你不能在 React 项目上安装 React Native 包。

此外,目前还没有针对 web 的 sqlite 实现。

但是,您可以使用本地存储将数据保存在客户端的浏览器中,或将数据库内容存储在服务器上。

于 2021-09-28T00:18:07.640 回答