InsertCat.js 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { DEBUG, enablePromise, openDatabase } from 'react-native-sqlite-storage';
import { SafeAreaView, StyleSheet, Text, View, TextInput, FlatList, Button } from 'react-native';
import React, { useEffect, useState } from 'react';

DEBUG(true);
enablePromise(true);

const db = openDatabase({
  name: 'MyMoney',
  location: 'default',
  createFromLocation: '~MyMoney.db', // android/src/main/assets/TestDB.db 파일을 위치 시킴
});

const QueryFunc = async (Query) => {
  (await db).transaction(Query)
};

function InsertCat() {
  const [cat, setCat] = useState('')
  const [asset, setAsset] = useState('')

  console.log('money db')

  const populateDatabase = async DB => {
    await DB.transaction(queryMoney); // 반드시 (await db)를 해야 프라미스가 성공
  };

  const loadAndQueryDB = async () => {
    try {
      console.log('load and db query ....');
      await populateDatabase(await db);
    } catch (error) {
      console.log(error);
    }
  };

  const insertCategory = async () => {
    QueryFunc(async (tx) => {
      console.log("카테고리 정보 저장");
      await tx.executeSql('INSERT INTO categories (category_name) VALUES (?);',
        [cat],
        () => { console.log("카테고리 삽입 성공"); },
        (error) => console.log(error))
    })
  }

  const insertAsset = async () => {
    QueryFunc(async (tx) => {
      console.log("자산 정보 저장");
      await tx.executeSql('INSERT INTO assets_type (assets_name) VALUES (?);',
        [asset],
        () => { console.log("자산 삽입 성공"); },
        (error) => console.log(error))
    })
  }


  return (
    <SafeAreaView>
      <Text>db test</Text>
      <View>
        <Text style={style.Font}>-데이터 입력 테스트</Text>
        <Text>cat</Text>
        <TextInput
          placeholder="cat"
          onChangeText={
            (cat) => setCat(cat)
          }
        />
        <Text>asset</Text>
        <TextInput
          placeholder="asset"
          onChangeText={
            (asset) => setAsset(asset)
          }
        />
      </View>
      <Button title='카테고리 삽입하기' onPress={insertCategory} />
      <Button title='자산 삽입하기' onPress={insertAsset} />
    </SafeAreaView>
  );
}

const style = StyleSheet.create({
  Font: {
    fontSize: 24
  }
});

export default InsertCat