SQLite.openDatabase('dbname') 不是 expo React Native 中的函数

问题描述 投票:0回答:1

这是我的编码

import * as SQLite from 'expo-sqlite';

const db = SQLite.openDatabase('myDatabase.db');

db.transaction((tx) => {
  tx.executeSql(
    'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)'
  );
})
  .then(() => {
    console.log('Table created successfully');
  })
  .catch((error) => {
    console.log('Error creating table:', error);
  });

我收到一条错误消息:SQLite.openDatabase 不是函数

我已经安装了软件包

npx expo install expo-sqlite

并导入

import * as SQLite from 'expo-sqlite';
react-native expo android-sqlite
1个回答
0
投票

如果您查看文档,https://docs.expo.dev/versions/latest/sdk/sqlite/,不再只有 openDatabase

SQLite.openDatabaseSync
(和
SQLite.openDatabaseAsync
)。所以我的猜测是将
openDatabase
更改为
openDatabaseSync
应该可以解决问题(尽管未经测试)。

© www.soinside.com 2019 - 2024. All rights reserved.