forked from jonasgomespe/react-native-sqlite-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
85 lines (76 loc) · 1.9 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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* Generated with the TypeScript template
* https://github.com/react-native-community/react-native-template-typescript
*
* @format
*/
import React, {useEffect, useState} from 'react';
import {Text, View, Button} from 'react-native';
import SQLite from 'react-native-sqlite-storage';
var db = SQLite.openDatabase(
{
location: 'default',
name: 'sqliteDb',
},
() => {
console.log('Tudo certo');
},
() => {
console.log('Errado');
},
);
const App = () => {
const [dadosBanco, setDadosBanco] = useState<any[]>([]);
useEffect(() => {
db.transaction(tx => {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS usuario (ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, password INTEGER)',
[],
);
});
}, []);
const inserindoValor: any = () => {
for (let i = 0; i < 4; i++) {
const name: string = 'Usuario ' + i;
const password: string = 'Senha ' + i;
db.transaction(vl => {
vl.executeSql(
'INSERT INTO usuario ( name, password ) VALUES (?, ?)',
[name, password],
() => {
console.log('Sucesso ao inserir');
},
);
});
}
};
const lerValor: any = () => {
db.transaction(vl => {
vl.executeSql('SELECT * FROM usuario', [], (req, result) => {
var dadosBancos: any[] = [];
for (let i = 0; i < result.rows.length; i++) {
dadosBancos.push(result.rows.item(i));
}
setDadosBanco(dadosBancos);
});
});
};
return (
<View>
<Button title="Inserir" onPress={inserindoValor} />
<Text />
<Button title="Ler" onPress={lerValor} />
{dadosBanco.map(val => {
return (
<Text key={val.ID}>
{val.name} - {val.password}
</Text>
);
})}
</View>
);
};
export default App;