import React, { Component } from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
// import { Constants, Asset, SQLite } from 'expo';
import * as FileSystem from 'expo-file-system';
import { Asset } from 'expo-asset';
import * as SQLite from 'expo-sqlite';
// Moved the database loading code to the App code,
// we need to be able to await promises and ideally
// to be able to inform user that something is happening.
export default class App extends Component {
state = {
// Inform the UI whether the database has been downloaded.
// Ideally we would also have an error property that would
// be able to contain an error that could occur while downloading
// the database -- we would show it to the user and allow him to retry the action.
downloaded: false,
};
componentDidMount() {
// Trigger downloading the database.
// Ideally we would probably put this whole downloading database code
// into a promise passed to AppLoading (https://docs.expo.io/versions/v28.0.0/sdk/app-loading)
this.downloadDatabase();
}
downloadDatabase = async () => {
const sqliteDirectory = `${FileSystem.documentDirectory}SQLite`;
// First, ensure that the SQLite directory is indeed a directory
// For that we will first get information about the filesystem node
// and handle non-existent scenario.
const { exists, isDirectory } = await FileSystem.getInfoAsync(
sqliteDirectory
);
if (!exists) {
await FileSystem.makeDirectoryAsync(sqliteDirectory);
} else if (!isDirectory) {
throw new Error('SQLite dir is not a directory');
}
const pathToDownloadTo = `${sqliteDirectory}/test.db`;
const uriToDownload = Asset.fromModule(require('./assets/db/test.db'))
.uri;
console.log(`Will download ${uriToDownload} to ${pathToDownloadTo}`);
// Let's download the file! We could have used something like
// https://github.com/expo/native-component-list/blob/3f03acb7e11a1b0cc0c33036743465aaae5c2cf1/screens/FileSystemScreen.js#L27-L44
// i. e. some progress indicator, but hey, that's just a demo!
await FileSystem.downloadAsync(uriToDownload, pathToDownloadTo);
this.db = SQLite.openDatabase('test.db');
// Once we've opened the database and saved the instance to `this`, we can enable the open button.
this.setState({ downloaded: true });
};
showTables = rows => {
const message = JSON.stringify(rows);
alert('Success:' + message);
};
loadDB = () => {
console.log('about to open db');
this.db.transaction(
tx => {
tx.executeSql(
`SELECT name FROM Test WHERE type='table'`,
// `SELECT * FROM states LIMIT 5`,
[],
(_, { rows }) => this.showTables(rows),
(txObj, error) => alert(error)
);
},
error => console.log('something went wrong:' + error),
() => console.log('db transaction is a success')
);
};
render() {
return (
<View style={styles.container}>
<Text style={styles.paragraph}>Testing SQL Database</Text>
<Button
onPress={() => this.loadDB()}
title={this.state.downloaded ? 'Click here' : 'Downloading...'}
disabled={!this.state.downloaded}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#ecf0f1',
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
color: '#34495e',
},
});
I can’t import data from test.db
My test.db file has Test table but when I import it to expo it creates test.db it compiles with no error and got the file in FileSystem.documentDirectory/SQLite/test.db but data in file doesn’t exist please help thank