Commit cad8ca7f authored by Choi Ga Young's avatar Choi Ga Young
Browse files

DB 해결

parent 39f4851e
import SQLite from 'react-native-sqlite-storage';
import { View, Text, StyleSheet, Button } from 'react-native';
import React, { useEffect } from 'react';
// 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';
SQLite.DEBUG(true);
SQLite.enablePromise(false);
DEBUG(true);
enablePromise(true);
const database_name = "MyMoney.db";
const database_version = "1.0"
const database_displayname = "SQLite Test Database";
const database_size = 200000;
let db;
// const database_name = "MyMoney.db";
// const database_version = "1.0"
// const database_displayname = "SQLite Test Database";
// const database_size = 200000;
function MoneyDB() {
const db = openDatabase({
name: 'MyMoney',
location: 'default',
createFromLocation: '~MyMoney.db',
});
console.log('money db')
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()
const loadAndQueryDB = () => {
db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, () => { console.log('load db') }, error => console.log(error.message));
const populateDatabase = async DB => {
await DB.transaction(getData); // 반드시 (await db)를 해야 프라미스가 성공
};
const loadAndQueryDB = async () => {
try {
console.log('load and db query ....');
await populateDatabase(await db);
} catch (error) {
console.log(error);
}
};
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 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 = () => {
db.transaction(tx => { console.log("테이블 생성하기"); tx.executeSql('CREATE TABLE IF NOT EXISTS Test(' + 'age INTEGER);', [], () => { console.log("테이블 생성 성공 "); }, (error) => console.log(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);
}
}
const insertData = () => {
db.transaction(tx => { console.log("데이터 삽입하기"); tx.executeSql('INSERT INTO Test (age) VALUES (23);', [], () => { console.log("삽입 성공"); }, (error) => console.log(error)) })
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()
}, [])
loadAndQueryDB();
}, []);
return (
<>
<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>
</>
)
}
export default MoneyDB
\ No newline at end of file
export default MoneyDB;
\ No newline at end of file
......@@ -188,10 +188,11 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+" // From node_modules
implementation project(':react-native-sqlite-storage')
implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation project(':react-native-sqlite-storage')
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
exclude group:'com.facebook.fbjni'
......
......@@ -4,6 +4,7 @@ import android.app.Application;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import org.pgsqlite.SQLitePluginPackage;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
......
rootProject.name = 'rich_maker'
include ':react-native-sqlite-storage'
project(':react-native-sqlite-storage').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sqlite-storage/platforms/android')
include ':react-native-sqlite-storage'
project(':react-native-sqlite-storage').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sqlite-storage/src/android')
apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
......
......@@ -12,6 +12,8 @@ target 'rich_maker' do
:hermes_enabled => false
)
pod 'react-native-sqlite-storage', :path => '../node_modules/react-native-sqlite-storage'
target 'rich_makerTests' do
inherit! :complete
# Pods for testing
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment