-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
61 lines (54 loc) · 1.77 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Asset } from 'expo-asset';
import * as FileSystem from 'expo-file-system';
import * as SQLite from 'expo-sqlite/next';
import { StatusBar } from 'expo-status-bar';
import { useEffect, useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
export default function App() {
const [row, setRow] = useState('');
const [error, setError] = useState('');
useEffect(() => {
queryDatabaseAsync()
.then((row) => setRow(JSON.stringify(row, null, 2)))
.catch((error) => setError(error.message));
}, []);
return (
<View style={styles.container}>
<Text>{row}</Text>
{error ? <Text style={{ color: 'red' }}>{error}</Text> : null}
<StatusBar style="auto" />
</View>
);
}
async function queryDatabaseAsync(): Promise<Record<string, any>> {
const localDbPath = FileSystem.documentDirectory + 'SQLite/myDatabaseName.db';
if (!(await FileSystem.getInfoAsync(localDbPath)).exists) {
if (
!(await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'SQLite'))
.exists
) {
await FileSystem.makeDirectoryAsync(
FileSystem.documentDirectory + 'SQLite'
);
}
const assetDb = require('./assets/myDatabaseName.db');
const asset = Asset.fromModule(assetDb);
if (!asset.uri) {
throw new Error(`Invalid asset: ${JSON.stringify(asset)}`);
}
await FileSystem.downloadAsync(
Asset.fromModule(assetDb).uri,
FileSystem.documentDirectory + 'SQLite/myDatabaseName.db'
);
}
const db = await SQLite.openDatabaseAsync('myDatabaseName.db');
return await db.getFirstAsync<any>('SELECT * FROM test');
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});