簡體   English   中英

與 SQLite 數據庫的 React Native 連接

[英]React Native connection with SQLite database

我是 React Native 的新手,我想知道我是否可以連接本地 SQLite 數據庫文件 (.db) 我遵循了 Github 上的一些指南,但它對我不起作用,還有其他方法可以做到嗎?? (使用安卓)

這是我說的指南: https : //github.com/andpor/react-native-sqlite-storage

global.db = SQLite.openDatabase(
  {
    name: "silkyMarket",
    location: "default",
    createFromLocation: "~SQLite.db",
  },
  () => {},
  (error) => {
    console.log("ERROR: " + error);
  }
);
function SettingsScreen() {
  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      <Text>Settings!</Text>
    </View>
  );
}

將其添加到您的應用程序的入口文件中,例如 index.js 的 app.js,然后您可以在其他文件中訪問它,如下所示:

import React from "react";
import SQLite from "react-native-sqlite-storage";

export default class SQLiteScreen extends React.Component {
  constructor() {
    super();
    SQLite.DEBUG = true;
  }

  ExecuteQuery = (sql, params = []) =>
    new Promise((resolve, reject) => {
      db.transaction((trans) => {
        trans.executeSql(
          sql,
          params,
          (trans, results) => {
            resolve(results);
          },
          (error) => {
            reject(error);
          }
        );
      });
    });

  // Create Table
  async CreateTable() {
    let Table = await this.executeQuery(
      "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY NOT NULL, first_name VARCHAR(16), last_name VARCHAR(16), is_deleted INTEGER)",
      []
    );
    console.log(Table);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM