-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
148 lines (135 loc) · 3.53 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-disable react/no-unstable-nested-components */
/* eslint-disable react-native/no-inline-styles */
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
*/
import React, {useEffect, useState} from 'react';
import {
FlatList,
SafeAreaView,
Text,
TouchableOpacity,
View,
} from 'react-native';
import SQLite from 'react-native-sqlite-storage';
const db = SQLite.openDatabase(
{name: 'example.db', location: 'default'},
() => {
console.log('___connect__success__');
},
e => {
console.log('e', e);
},
);
function App(): JSX.Element {
const [users, setUsers] = useState<any[] | null>(null);
useEffect(() => {
createTable();
}, []);
const createTable = () => {
db.transaction(
tx => {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS Users (_id integer primary key not null, Name TEXT, Age INTERGER);',
);
},
e => {
console.log('___e___create table___', e);
},
);
};
const addData = async (name: string, age: number) => {
console.log('__add_func___', name, age);
await db.transaction(async tx => {
await tx.executeSql(
'INSERT INTO Users (Name,Age) VALUES (?,?)',
[name, age],
() => {},
e => {
console.log('__err__add___', e);
},
);
console.log('__add__success');
});
};
const getData = async () => {
await db.transaction(
async tx => {
await tx.executeSql('SELECT * FROM Users', [], (tx, res) => {
var leng = res.rows.length;
let data = [];
if (leng) {
for (let index = 0; index < leng; index++) {
const element = res.rows.item(index);
data.push(element);
}
setUsers(data);
}
});
console.log('__get_success___');
},
e => {
console.log('__err__get___', e);
},
);
};
const randomData = async () => {
const random = Math.floor(Math.random() * 100);
await addData(`name ${random}`, random);
};
type ItemProps = {Name: string; Age: number; _id: number};
const Item = ({Name, Age}: ItemProps) => (
<View style={{paddingVertical: 6, flexDirection: 'row'}}>
<Text style={{flex: 1}}>{Name}</Text>
<Text style={{flex: 1}}>{Age}</Text>
</View>
);
return (
<SafeAreaView style={{flex: 1, paddingHorizontal: 10}}>
<View
style={{
flexDirection: 'row',
justifyContent: 'space-between',
marginVertical: 10,
}}>
<TouchableOpacity
style={{
flex: 1,
height: 50,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 10,
backgroundColor: 'tomato',
}}
onPress={randomData}>
<Text>add</Text>
</TouchableOpacity>
<View style={{width: 10}} />
<TouchableOpacity
style={{
flex: 1,
height: 50,
justifyContent: 'center',
alignItems: 'center',
borderRadius: 10,
backgroundColor: 'lightgreen',
}}
onPress={getData}>
<Text>get</Text>
</TouchableOpacity>
</View>
<FlatList
data={users}
style={{flex: 1}}
renderItem={({item}: {item: ItemProps}) => (
<Item Name={item.Name} Age={item.Age} _id={item._id} />
)}
keyExtractor={item => item._id.toString()}
/>
</SafeAreaView>
);
}
export default App;