· 7 years ago · Sep 11, 2018, 12:22 AM
1import React, { Component } from 'react';
2import { Text, View, ScrollView, StyleSheet, TouchableOpacity } from 'react-native';
3import { Constants, SQLite } from 'expo';
4
5const db = SQLite.openDatabase('db.db');
6
7export default class Splash extends React.Component {
8
9 constructor(props) {
10 super(props);
11 this.state = {
12 items: []
13 };
14 }
15
16 componentDidMount() {
17 // db.transaction((tx) => {
18 // tx.executeSql('create table if not exists items (id integer not null primary key, done int, value text);', [],
19 // function (transaction, resultSet) {
20 // console.log('created table', resultSet);
21 // this.select()
22 // }, function (transaction, error) {
23 // console.log('Error create table', error)
24 // });
25 // });
26 this.select()
27 }
28 init = async () => {
29 await this.executeSql('create table if not exists locations (latitude numeric, longitude numeric);');
30 this.insert()
31 };
32
33 executeSql = async (sql, params = []) => {
34 return new Promise((resolve, reject) => db.transaction(tx => {
35 tx.executeSql(sql, params, (_, { rows }) => resolve(rows._array), reject)
36 }))
37 };
38
39 _insert = async () => {
40 await this.executeSql('insert into locations (latitude, longitude) values (?, ?)', [-23.426498, -51.938130]);
41 await this.executeSql('insert into locations (latitude, longitude) values (?, ?)', [null, null]);
42 await this.executeSql('insert into locations (latitude, longitude) values (?, ?)', [-23.410068, -51.937695]);
43 return true
44 };
45
46 insert = () => {
47 this._insert().then(this.select)
48 };
49
50 clear = () => {
51 this.executeSql('delete from locations').then(this.select);
52 };
53 select = () => {
54 this.executeSql('select * from locations', []).then(result => this.setState({items: result}) );
55 };
56
57 render() {
58 return (
59 <View style={styles.container}>
60 <Text>{JSON.stringify(this.state.items)}</Text>
61 </View>
62 );
63 }
64}
65
66const styles = StyleSheet.create({
67 container: {
68 flex: 1,
69 alignItems: 'center',
70 justifyContent: 'center',
71 paddingTop: Constants.statusBarHeight,
72 backgroundColor: '#ecf0f1',
73 },
74 paragraph: {
75 margin: 24,
76 fontSize: 18,
77 fontWeight: 'bold',
78 textAlign: 'center',
79 color: '#34495e',
80 },
81});