React Native Running on Expo - SQLite Error when using Pre-populated Database - TypeError: undefined is not a function (near '...db.transaction...')
React Native Running on Expo - SQLite Error when using Pre-populated Database - TypeError: undefined is not a function (near '...db.transaction...')
我在 Expo 上使用 React Native 和 SQlite 数据库,目标是创建一个离线 Android 应用程序。数据库已预先填充,因此我已将其复制到 assets/database/FarmersDB.db
文件夹。我已经阅读了官方文档以及提供的建议 here 和其他提供的解决方案,但最终出现以下错误。
TypeError: undefined is not a function (near '...db.transaction...')
我的代码如下:
//Login Component
import React, {useEffect, useState} from 'react'
import * as SQLite from 'expo-sqlite';
import * as FileSystem from 'expo-file-system'
import {Asset } from 'expo-asset'
async function openDb() {
if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + "SQLite")).exists) {
await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + "SQLite");
}
await FileSystem.downloadAsync(
Asset.fromModule(require("../assets/database/FarmerDB.db")).uri,
FileSystem.documentDirectory + "SQLite/FarmerDB.db"
);
return SQLite.openDatabase("FarmerDB.db","1.0");
}
const LoginScreen = () => {
useEffect(() => {
const db = openDb()
db.transaction((tx) =>{
tx.executeSql(
"SELECT * FROM Farmer WHERE id = 1",
[],
(tx,results) =>{
console.log("success")
}
)
})
}, []);
}
项目根目录下的metro.config.js文件如下
const { getDefaultConfig } = require('expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
module.exports = {
resolver: {
assetExts: [...defaultConfig.resolver.assetExts,'db'],
},
};
经过一番努力,我得到了解决方案,
const db = openDb() //This returns a promise
所以代码应该如下所示。
const LoginScreen = () => {
useEffect(() => {
openDb()
.then(db =>
db.transaction((tx) =>{
tx.executeSql(
"SELECT * FROM Farmer WHERE id = 1",
[],
(tx,results) =>{
console.log("success")
})
)
})
}, []);
}
我在 Expo 上使用 React Native 和 SQlite 数据库,目标是创建一个离线 Android 应用程序。数据库已预先填充,因此我已将其复制到 assets/database/FarmersDB.db
文件夹。我已经阅读了官方文档以及提供的建议 here 和其他提供的解决方案,但最终出现以下错误。
TypeError: undefined is not a function (near '...db.transaction...')
我的代码如下:
//Login Component
import React, {useEffect, useState} from 'react'
import * as SQLite from 'expo-sqlite';
import * as FileSystem from 'expo-file-system'
import {Asset } from 'expo-asset'
async function openDb() {
if (!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + "SQLite")).exists) {
await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + "SQLite");
}
await FileSystem.downloadAsync(
Asset.fromModule(require("../assets/database/FarmerDB.db")).uri,
FileSystem.documentDirectory + "SQLite/FarmerDB.db"
);
return SQLite.openDatabase("FarmerDB.db","1.0");
}
const LoginScreen = () => {
useEffect(() => {
const db = openDb()
db.transaction((tx) =>{
tx.executeSql(
"SELECT * FROM Farmer WHERE id = 1",
[],
(tx,results) =>{
console.log("success")
}
)
})
}, []);
}
项目根目录下的metro.config.js文件如下
const { getDefaultConfig } = require('expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
module.exports = {
resolver: {
assetExts: [...defaultConfig.resolver.assetExts,'db'],
},
};
经过一番努力,我得到了解决方案,
const db = openDb() //This returns a promise
所以代码应该如下所示。
const LoginScreen = () => {
useEffect(() => {
openDb()
.then(db =>
db.transaction((tx) =>{
tx.executeSql(
"SELECT * FROM Farmer WHERE id = 1",
[],
(tx,results) =>{
console.log("success")
})
)
})
}, []);
}