'save db data in a variable (array) (Expo react native sqlite)
I receive information from the database and I want to pass this information to another screen, through the route parameter. It's possible? The way I thought didn't work, I'm new to react native, thanks for any help. i use sqlite expo.
My app has a login screen (which authenticates through the tb_user db table), which goes to another screen to select a vehicle. I have to save the vehicle data (which the user wrote) in a table (tb_veic_user), informing which of the users made the modification.
MY TABLE USER ----> CREATE TABLE IF NOT EXISTS tb_user (ID_user INTEGER PRIMARY KEY, username TEXT, senha TEXT, ID_empresa INT, nome_empresa TEXT, status_user INT);
MY TABLE VEIC ----> CREATE TABLE IF NOT EXISTS tb_veic_user (ID_veic_uso INTEGER PRIMARY KEY AUTOINCREMENT, ID_user INT, placa TEXT, atividade TEXT, data_inic TEXT , km_inic INT, data_final TEXT, km_final INT, integrado INT);
//LOGIN.js (the login button calls this function 'login')
function Login({ navigation, route }) {
const [username, setUsername] = useState("")
const [password, setPassword] = useState("")
const [errorLogin, setErrorLogin] = useState("")
const [users, setUsers] = useState([])
const [ID_user, setID_user] = useState([])
const login = () => {
/**
* FAZER LOGIN
* - Pega tudo de user ONDE username seja "" e senha seja "";
*/
return new Promise((resolve, reject) => {
db.transaction(tx => {
tx.executeSql(
'SELECT ID_user, username, senha FROM tb_user WHERE username = ? and senha = ? LIMIT 1;',
[username, password],
//-----------------------
(_, { rows }) => {
if (rows.length > 0){
resolve(rows._array),
navigation.navigate('Veic', {
paramKey: username,
id: ID_user
}),
console.log(rows._array)
}else{ reject("Obj not found");} // nenhum registro encontrado
},
(_, error) => reject(error) // Success
);
});
});
}
return (
<View style={fundoLogin.container}>
<ImageBackground
source={require('../../imagens/1.jpg')}
style={fundoLogin.imageFundo}
>
<Image
style={fundoLogin.imageLogo}
source={require('../../imagens/logo_cymi.png')}
/>
<TextInput
style={fundoLogin.input}
placeholder='Digite seu email'
onChangeText={txtUsername => setUsername(txtUsername)}
value={username}
/>
<TextInput
style={fundoLogin.input}
placeholder='Digite sua senha'
secureTextEntry={true}
onChangeText={txtPassword => setPassword(txtPassword)}
value={password}
/>
<Pressable
style={fundoLogin.button}
onPress={login}
>
<Text style={fundoLogin.textButton}>Login</Text>
</Pressable>
}
</ImageBackground>
</View>
);
}
export default Login;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
