Last active
February 22, 2024 11:56
-
-
Save juhahinkula/a010afc9d3f663cb1ea4bec818a31135 to your computer and use it in GitHub Desktop.
React Native SQLite Courselist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react'; | |
import { StyleSheet, Text, TextInput, View, Button, FlatList } from 'react-native'; | |
import * as SQLite from 'expo-sqlite'; | |
const db = SQLite.openDatabase('coursedb.db'); | |
export default function App() { | |
const [credit, setCredit] = useState(''); | |
const [title, setTitle] = useState(''); | |
const [courses, setCourses] = useState([]); | |
useEffect(() => { | |
db.transaction(tx => { | |
tx.executeSql('create table if not exists course (id integer primary key not null, credits int, title text);'); | |
}, null, updateList); | |
}, []); | |
// Save course | |
const saveItem = () => { | |
db.transaction(tx => { | |
tx.executeSql('insert into course (credits, title) values (?, ?);', [parseInt(credit), title]); | |
}, null, updateList | |
) | |
} | |
// Update courselist | |
const updateList = () => { | |
db.transaction(tx => { | |
tx.executeSql('select * from course;', [], (_, { rows }) => | |
setCourses(rows._array) | |
); | |
}); | |
} | |
// Delete course | |
const deleteItem = (id) => { | |
db.transaction( | |
tx => { | |
tx.executeSql(`delete from course where id = ?;`, [id]); | |
}, null, updateList | |
) | |
} | |
const listSeparator = () => { | |
return ( | |
<View | |
style={{ | |
height: 5, | |
width: "80%", | |
backgroundColor: "#fff", | |
marginLeft: "10%" | |
}} | |
/> | |
); | |
}; | |
return ( | |
<View style={styles.container}> | |
<TextInput placeholder='Title' style={{marginTop: 30, fontSize: 18, width: 200, borderColor: 'gray', borderWidth: 1}} | |
onChangeText={(title) => setTitle(title)} | |
value={title}/> | |
<TextInput placeholder='Credits' keyboardType="numeric" style={{ marginTop: 5, marginBottom: 5, fontSize:18, width: 200, borderColor: 'gray', borderWidth: 1}} | |
onChangeText={(credit) => setCredit(credit)} | |
value={credit}/> | |
<Button onPress={saveItem} title="Save" /> | |
<Text style={{marginTop: 30, fontSize: 20}}>Courses</Text> | |
<FlatList | |
style={{marginLeft : "5%"}} | |
keyExtractor={item => item.id.toString()} | |
renderItem={({item}) => <View style={styles.listcontainer}><Text style={{fontSize: 18}}>{item.title}, {item.credits}</Text> | |
<Text style={{fontSize: 18, color: '#0000ff'}} onPress={() => deleteItem(item.id)}> Done</Text></View>} | |
data={courses} | |
ItemSeparatorComponent={listSeparator} | |
/> | |
</View> | |
); | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: '#fff', | |
alignItems: 'center', | |
justifyContent: 'center', | |
}, | |
listcontainer: { | |
flexDirection: 'row', | |
backgroundColor: '#fff', | |
alignItems: 'center' | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment