Skip to content

Instantly share code, notes, and snippets.

@MatheusPimentel
Created September 10, 2018 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatheusPimentel/7c670b4557ee98c0f5ad42a8111415ec to your computer and use it in GitHub Desktop.
Save MatheusPimentel/7c670b4557ee98c0f5ad42a8111415ec to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { Text, View, ScrollView, StyleSheet, TouchableOpacity } from 'react-native';
import { Constants, SQLite } from 'expo';
const db = SQLite.openDatabase('db.db');
export default class Splash extends React.Component {
constructor(props) {
super(props);
this.state = {
items: []
};
}
componentDidMount() {
// db.transaction((tx) => {
// tx.executeSql('create table if not exists items (id integer not null primary key, done int, value text);', [],
// function (transaction, resultSet) {
// console.log('created table', resultSet);
// this.select()
// }, function (transaction, error) {
// console.log('Error create table', error)
// });
// });
this.select()
}
init = async () => {
await this.executeSql('create table if not exists locations (latitude numeric, longitude numeric);');
this.insert()
};
executeSql = async (sql, params = []) => {
return new Promise((resolve, reject) => db.transaction(tx => {
tx.executeSql(sql, params, (_, { rows }) => resolve(rows._array), reject)
}))
};
_insert = async () => {
await this.executeSql('insert into locations (latitude, longitude) values (?, ?)', [-23.426498, -51.938130]);
await this.executeSql('insert into locations (latitude, longitude) values (?, ?)', [null, null]);
await this.executeSql('insert into locations (latitude, longitude) values (?, ?)', [-23.410068, -51.937695]);
return true
};
insert = () => {
this._insert().then(this.select)
};
clear = () => {
this.executeSql('delete from locations').then(this.select);
};
select = () => {
this.executeSql('select * from locations', []).then(result => this.setState({items: result}) );
};
render() {
return (
<View style={styles.container}>
<Text>{JSON.stringify(this.state.items)}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
},
paragraph: {
margin: 24,
fontSize: 18,
fontWeight: 'bold',
textAlign: 'center',
color: '#34495e',
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment