MoneyDB.js 2.82 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
2
3
4
// import SQLite, { openDatabase } from 'react-native-sqlite-storage';
import { DEBUG, enablePromise, openDatabase } from 'react-native-sqlite-storage';
import { View, SafeAreaView, Text, StyleSheet, Button, TextInput } from 'react-native';
import React, { useEffect, useState } from 'react';
Choi Ga Young's avatar
Choi Ga Young committed
5

Choi Ga Young's avatar
Choi Ga Young committed
6
7
DEBUG(true);
enablePromise(true);
Choi Ga Young's avatar
Choi Ga Young committed
8

Choi Ga Young's avatar
Choi Ga Young committed
9
10
11
12
13
const db = openDatabase({
  name: 'MyMoney',
  location: 'default',
  createFromLocation: '~MyMoney.db',
});
Choi Ga Young's avatar
Choi Ga Young committed
14

Choi Ga Young's avatar
Choi Ga Young committed
15
16
17
18
19
20
21
22
23
function MoneyDB() {
  const [date, setDate] = useState('2021-01-01')
  const [category, setCategory] = useState(2)
  const [subcategory, setSubcategory] = useState(2)
  const [type, setType] = useState('우리')
  const [contents, setContents] = useState('경로')
  const [price, setPrice] = useState(1000)
  const [assetsId, setAssetsId] = useState()
  const [assetsname, setAssetsname] = useState()
Choi Ga Young's avatar
Choi Ga Young committed
24

Choi Ga Young's avatar
Choi Ga Young committed
25
26
27
  const populateDatabase = async DB => {
    await DB.transaction(getData); // 반드시 (await db)를 해야 프라미스가 성공
  };
Choi Ga Young's avatar
Choi Ga Young committed
28

Choi Ga Young's avatar
Choi Ga Young committed
29
30
31
32
33
34
35
  const loadAndQueryDB = async () => {
    try {
      console.log('load and db query ....');
      await populateDatabase(await db);
    } catch (error) {
      console.log(error);
    }
Choi Ga Young's avatar
Choi Ga Young committed
36
37
  };

Choi Ga Young's avatar
Choi Ga Young committed
38
39
40
41
42
43
44
45
  const getData = async tx => {
    console.log('데이터 가져오기');
    try {
      const [txn, results] = await tx.executeSql('SELECT * FROM money');
      console.log('results: ', results.rows.item(0));
    } catch (error) {
      console.log('error in getData', error);
    }
Choi Ga Young's avatar
Choi Ga Young committed
46
47
  }

Choi Ga Young's avatar
Choi Ga Young committed
48
49
50
51
52
53
54
55
56

  const createTable = async tx => {
    console.log('테이블 생성하기');
    try {
      const [txn, results] = await tx.executeSql('CREATE TABLE IF NOT EXISTS Test(' + 'Money INTEGER);');
      console.log("테이블 생성 성공 ");
    } catch (error) {
      console.log('error in createTable', error);
    }
Choi Ga Young's avatar
Choi Ga Young committed
57
58
  }

Choi Ga Young's avatar
Choi Ga Young committed
59
60
61
62
63
64
65
66
67
68
69
70
  const insertData = async () => {
    try {
      (await db).transaction((tx) => {
        console.log("데이터 삽입하기");
        tx.executeSql('INSERT INTO assets_type (assets_id, assets_name) VALUES (?,?);',
          [assetsId, assetsname],
          () => { console.log("삽입 성공"); },
          (error) => console.log(error))
      })
    } catch (error) {
      console.log('error in insertData', error);
    }
Choi Ga Young's avatar
Choi Ga Young committed
71
  }
Choi Ga Young's avatar
Choi Ga Young committed
72

Choi Ga Young's avatar
Choi Ga Young committed
73
74
75
  useEffect(() => {
    loadAndQueryDB();
  }, []);
Choi Ga Young's avatar
Choi Ga Young committed
76
77
78

  return (
    <>
Choi Ga Young's avatar
Choi Ga Young committed
79
80
81
82
83
84
85
86
87
88
89
90
91
      <SafeAreaView>
        <View>
          <Text>DB</Text>
          <Button title='데이터 가져오기' onPress={() => getData()} />
          <Button title='테이블 생성하기' onPress={() => createTable()} />

          <TextInput placeholder='assetsId' onChangeText={(assetsId) => setAssetsId(assetsId)} />
          <TextInput placeholder='assetsname' onChangeText={(assetsname) => setAssetsname(assetsname)} />

          <Button title='데이터 삽입하기' onPress={() => insertData()} />
        </View>
      </SafeAreaView>

Choi Ga Young's avatar
Choi Ga Young committed
92
93
94
95
96
    </>
  )
}


Choi Ga Young's avatar
Choi Ga Young committed
97
export default MoneyDB;