I am new to react native, I am trying to connect with sqlite but when doing the transaction I get the following error, I have put some console.log, but it cannot enter after the transaction.
import { useEffect, useState } from "react";
import { Text, View } from "react-native";
import SQLite from "react-native-sqlite-storage";
export default function ListProducts(props: any) {
const [names, setNames] = useState<string[]>([]); // Asegúrate de especificar el tipo correcto para names
useEffect(() => {
const initializeDatabase = async () => {
try {
const db = await SQLite.openDatabase({
name: "mydata.db",
createFromLocation: 'default'
});
db.transaction(tx => {
// Crear la tabla si no existe
tx.executeSql('CREATE TABLE IF NOT EXISTS products (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)');
// Consulta SELECT para obtener los nombres de los productos
tx.executeSql(
"SELECT * FROM products",
[],
(_, result) => {
// Obtener los nombres y actualizar el estado
const productNames = Array.from(result.rows).map(row => row.name);
setNames(productNames);
},
(_, error) => {
console.error("Error al consultar productos:", error);
}
);
});
} catch (error) {
console.error("Error al abrir la base de datos:", error);
}
};
initializeDatabase();
}, []);
return (
// Renderiza tu componente aquí
<View>
{names.map((name, index) => (
<Text key={index}>{name}</Text>
))}
</View>
);
}
I hope I can execute the transaction well and make queries to sqlite
New contributor
Oscar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2