InsertCat.js 2.07 KB
Newer Older
1
import { DEBUG, enablePromise, openDatabase } from 'react-native-sqlite-storage';
Choi Ga Young's avatar
Choi Ga Young committed
2
3
import { SafeAreaView, StyleSheet, Text, View, TextInput, Button } from 'react-native';
import React, { useState } from 'react';
4
5
6
7
8
9
10

DEBUG(true);
enablePromise(true);

const db = openDatabase({
  name: 'MyMoney',
  location: 'default',
Choi Ga Young's avatar
Choi Ga Young committed
11
  createFromLocation: '~MyMoney.db',
12
13
14
15
16
17
18
19
20
21
22
23
24
});

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 => {
Choi Ga Young's avatar
Choi Ga Young committed
25
    await DB.transaction(queryMoney); 
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
  };

  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) => {
      await tx.executeSql('INSERT INTO categories (category_name) VALUES (?);',
        [cat],
        () => { console.log("카테고리 삽입 성공"); },
        (error) => console.log(error))
    })
  }

  const insertAsset = async () => {
    QueryFunc(async (tx) => {
      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