MoneyDB.js 2.98 KB
Newer Older
YoonDongMin's avatar
YdM    
YoonDongMin 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

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

YoonDongMin's avatar
YdM    
YoonDongMin committed
9
10
11
12
13
14
15
16
17
18
// const database_name = "MyMoney.db";
// const database_version = "1.0"
// const database_displayname = "SQLite Test Database";
// const database_size = 200000;

const db = openDatabase({
  name: 'MyMoney',
  location: 'default',
  createFromLocation: '~MyMoney.db',
});
Choi Ga Young's avatar
Choi Ga Young committed
19
20

function MoneyDB() {
YoonDongMin's avatar
YdM    
YoonDongMin committed
21
22
23
24
25
26
27
28
  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
29

YoonDongMin's avatar
YdM    
YoonDongMin committed
30
31
32
  const populateDatabase = async DB => {
    await DB.transaction(getData); // 반드시 (await db)를 해야 프라미스가 성공
  };
Choi Ga Young's avatar
Choi Ga Young committed
33

YoonDongMin's avatar
YdM    
YoonDongMin committed
34
35
36
37
38
39
40
  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
41
42
  };

YoonDongMin's avatar
YdM    
YoonDongMin committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  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);
    }
  }


  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
63

YoonDongMin's avatar
YdM    
YoonDongMin committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  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);
    }
  }

  useEffect(() => {
    loadAndQueryDB();
  }, []);
Choi Ga Young's avatar
Choi Ga Young committed
81
82
83

  return (
    <>
YoonDongMin's avatar
YdM    
YoonDongMin committed
84
85
86
87
88
89
90
91
92
93
94
95
96
      <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
97
98
99
100
101
    </>
  )
}


YoonDongMin's avatar
YdM    
YoonDongMin committed
102
export default MoneyDB;