Component - SQLite with expo-sqlite
This component shows how to use SQLite database in a React Native app using the expo-sqlite library. It opens a database, creates a table, inserts a record, and displays stored data on the screen.
This component shows how to use SQLite database in a React Native app using the expo-sqlite library. It opens a database, creates a table, inserts a record, and displays stored data on the screen.
View ├─ Text (Title) ├─ Button (Add Item) └─ FlatList (Data List) └─ Text (List Item)
expo-sqlite in a React Native app?openDatabase.openDatabase, which is the correct syntax.const db = SQLite.openDatabase('test.db');
db.transaction(tx => {
tx.executeSql('CREATE TABLE IF NOT EXISTS items (id INTEGER PRIMARY KEY NOT NULL, name TEXT);');
tx.executeSql('INSERT INTO items (name) VALUES (?)', ['apple']);
tx.executeSql('SELECT * FROM items;', [], (_, { rows }) => {
console.log(rows._array);
});
});db.transaction(tx => {
tx.executeSql('INSERT INTO users (name, age) VALUES (?, ?)', ['John']);
});const [items, setItems] = useState([]);
useEffect(() => {
db.transaction(tx => {
tx.executeSql('SELECT * FROM items;', [], (_, { rows }) => {
// What should go here?
});
});
}, []);setItems must be called with new data.items or returning data won't update state; logging only shows data.Pass the test and this pattern moves unknown