MoneyDB.js 1.73 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import SQLite from 'react-native-sqlite-storage';
Choi Ga Young's avatar
Choi Ga Young committed
2
import { View, Text, StyleSheet, Button } from 'react-native';
Choi Ga Young's avatar
Choi Ga Young committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import React, { useEffect } from 'react';

SQLite.DEBUG(true);
SQLite.enablePromise(false);

const database_name = "MyMoney.db";
const database_version = "1.0"
const database_displayname = "SQLite Test Database";
const database_size = 200000;
let db;

function MoneyDB() {

  console.log('money db')

  const loadAndQueryDB = () => {
    db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, () => { console.log('load db') }, error => console.log(error.message));
Choi Ga Young's avatar
Choi Ga Young committed
20

Choi Ga Young's avatar
Choi Ga Young committed
21
22
  };

Choi Ga Young's avatar
Choi Ga Young committed
23
24
25
26
27
28
29
30
31
32
33
  const getData = () => {
    db.transaction(tx => { console.log("데이터 가져오기"); tx.executeSql('SELECT * FROM Test;', [], (tx, results) => { console.log("select "); console.log('results:',results.rows.row) }, (error) => console.log(error)) })
  }

  const createTable = () => {
    db.transaction(tx => { console.log("테이블 생성하기"); tx.executeSql('CREATE TABLE IF NOT EXISTS Test(' + 'age INTEGER);', [], () => { console.log("테이블 생성 성공 "); }, (error) => console.log(error)) })
  }

  const insertData = () => {
    db.transaction(tx => { console.log("데이터 삽입하기"); tx.executeSql('INSERT INTO Test (age) VALUES (23);', [], () => { console.log("삽입 성공"); }, (error) => console.log(error)) })
  }
Choi Ga Young's avatar
Choi Ga Young committed
34

Choi Ga Young's avatar
Choi Ga Young committed
35
36
37
    useEffect(() => {
      loadAndQueryDB()
    }, [])
Choi Ga Young's avatar
Choi Ga Young committed
38
39
40
41
42

  return (
    <>
      <View>
        <Text>DB</Text>
Choi Ga Young's avatar
Choi Ga Young committed
43
44
45
        <Button title='데이터 가져오기' onPress={() => getData()} />
        <Button title='테이블 생성하기' onPress={() => createTable()} />
        <Button title='데이터 삽입하기' onPress={() => insertData()} />
Choi Ga Young's avatar
Choi Ga Young committed
46
47
48
49
50
51
52
      </View>
    </>
  )
}


export default MoneyDB