I'm creating this table:
export function createTable() {
db.transaction((transaction) => {
transaction.executeSql("CREATE TABLE IF NOT EXISTS " +
"Brand" +
"(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, brand TEXT NOT NULL, type TEXT NOT NULL);")
})
}
and then i'm filling it up with a ton of values:
export async function addBrand(){
return new Promise((resolve) => {
db.transaction((transaction) => {
transaction.executeSql("INSERT INTO Brand (brand, type) VALUES (Abarth, Car), " +
"(Acura, Car), " +
"(Alfa Romeo, Car), " +
"(Aston Martin, Car), " +
"(Audi, Car), " +
"(BMW, Motorcycle);", // it continues for more a hundred lines.
() => {
resolve("Marcas adicionadas com sucesso!")
})
})
})
}
I'm tryng to search this data with this function
export async function searchBrand(vehicleType){
return new Promise((resolve) => {
db.transaction((transaction) => {
transaction.executeSql("SELECT brand FROM Brand WHERE type LIKE (?);",
[vehicleType], (transaction, resultado) => {
resolve(resultado.rows._array)
})
})
})
}
and in another page i'm importing and calling the previous function
async function showBrand() {
const brands= await searchBrand(vehicleType)
console.log("Showing brands")
setFilteredBrands(brands)
}
and this beeing called for a Effect hook in the same page:
useEffect(() => {
showBrand()
}, [vehicleType]) // vehicleType is another state in my code, it is working as it should
My goal is to use setFilteredBrands(brands) to fill the state with strings, that is beeing pulled from my searchBrand function.
const [filteredBrands, setFilteredBrands] = useState([])
I need to fill this array with strings (only data from my brandcolunm from Brands table where the type is Car or other that is in the other state), so i can use
{
filteredBrands.map(brand => {
return <Picker.Item label={brand} value={brand}/>
})
}
and then it should show a list i my Picker so the user can choose in the form.
Althoug, it's simply not working. Only appears itens in the Picker if I fill the state manually.
Like this const [filteredBrands, setFilteredBrands] = useState(["Honda", "Maseratti", "BMW"])
The console.log("Showing brands") is not being executed, in other words, the function above
const brands= await searchBrand(vehicleType)
never ends. This way my Picker is empty.
What do I do? What is wrong with my code?
I don't know what to do, any help will be welcome!