Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
students
AwesomeProject
Commits
a6710cae
Commit
a6710cae
authored
Jul 01, 2021
by
Test User
Browse files
코드 정리
parent
f5bdfb1f
Changes
1
Hide whitespace changes
Inline
Side-by-side
App.js
View file @
a6710cae
...
@@ -6,8 +6,8 @@
...
@@ -6,8 +6,8 @@
* @flow strict-local
* @flow strict-local
*/
*/
import
React
,
{
useEffect
,
use
Ref
}
from
'
react
'
;
import
React
,
{
useEffect
,
use
State
}
from
'
react
'
;
import
{
SafeAreaView
,
StyleSheet
,
Text
}
from
'
react-native
'
;
import
{
SafeAreaView
,
StyleSheet
,
Text
,
View
,
FlatList
}
from
'
react-native
'
;
import
{
DEBUG
,
enablePromise
,
openDatabase
}
from
'
react-native-sqlite-storage
'
;
import
{
DEBUG
,
enablePromise
,
openDatabase
}
from
'
react-native-sqlite-storage
'
;
DEBUG
(
true
);
DEBUG
(
true
);
...
@@ -16,75 +16,35 @@ enablePromise(true);
...
@@ -16,75 +16,35 @@ enablePromise(true);
const
db
=
openDatabase
({
const
db
=
openDatabase
({
name
:
'
TestDB
'
,
name
:
'
TestDB
'
,
location
:
'
default
'
,
location
:
'
default
'
,
createFromLocation
:
'
~TestDB.db
'
,
createFromLocation
:
'
~TestDB.db
'
,
// android/src/main/assets/TestDB.db 파일을 위치 시킴
});
});
const
App
=
()
=>
{
const
App
=
()
=>
{
const
dbRef
=
useRef
(
null
);
const
[
users
,
setUsers
]
=
useState
([]
);
const
populateDatabase
=
async
db
=>
{
const
populateDatabase
=
async
DB
=>
{
// await db.executeSql('SELECT 1 FROM Version LIMIT 1');
await
DB
.
transaction
(
queryUser
);
// 반드시 (await db)를 해야 프라미스가 성공
(
await
db
).
transaction
(
queryUser
);
};
};
const
loadAndQueryDB
=
async
()
=>
{
const
loadAndQueryDB
=
async
()
=>
{
try
{
try
{
console
.
log
(
'
load and db query ....
'
);
console
.
log
(
'
load and db query ....
'
);
// db.transaction(function (txn) {
await
populateDatabase
(
await
db
);
// txn.executeSql(
// "SELECT name FROM sqlite_master WHERE type='table' AND name='tbl_user'",
// [],
// function (tx, res) {
// console.log('item:', res.rows.length);
// if (res.rows.length == 0) {
// txn.executeSql('DROP TABLE IF EXISTS tbl_user', []);
// txn.executeSql(
// 'CREATE TABLE IF NOT EXISTS tbl_user(user_id INTEGER PRIMARY KEY AUTOINCREMENT, user_name VARCHAR(20), user_contact INT(10), user_address VARCHAR(255))',
// [],
// );
// }
// },
// );
// });
// db.transaction(function (tx) {
// console.log('transaction started....');
// tx.executeSql(
// 'select * from users',
// [],
// function (tx, res) {
// console.log('item length', res.rows.length);
// for (let i = 0; i < res.rows.length; i++) {
// const element = res.rows.item(i);
// console.log('element=', element);
// }
// },
// function (error) {
// console.log(error);
// },
// );
// });
// SQLite.openDatabase({
// name: 'TestDB',
// location: 'default',
// createFromLocation: '~TestDB.db',
// }).then(db => {
// dbRef.current = db;
// db.transaction(tx => tx.executeSql('select * from users', []));
// // populateDatabase(db);
// });
// dbRef.current = db;
await
populateDatabase
(
db
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
log
(
error
);
console
.
log
(
error
);
}
}
};
};
const
closeDatabase
=
async
db
=>
{
const
closeDatabase
=
async
DB
=>
{
if
(
db
)
{
if
(
DB
)
{
console
.
log
(
'
closing database ...
'
);
console
.
log
(
'
closing database ...
'
);
try
{
try
{
await
db
.
close
();
(
await
DB
).
close
(
console
.
log
(
'
Database closed
'
);
()
=>
{
console
.
log
(
'
Database was closed successfully
'
);
},
err
=>
console
.
log
(
err
),
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
log
(
error
);
console
.
log
(
error
);
}
}
...
@@ -95,14 +55,16 @@ const App = () => {
...
@@ -95,14 +55,16 @@ const App = () => {
const
queryUser
=
async
tx
=>
{
const
queryUser
=
async
tx
=>
{
console
.
log
(
'
Excuting user query
'
);
console
.
log
(
'
Excuting user query
'
);
console
.
log
(
'
tx
'
,
tx
);
try
{
try
{
const
[
txn
,
results
]
=
await
tx
.
executeSql
(
'
SELECT * FROM users
'
);
const
[
txn
,
results
]
=
await
tx
.
executeSql
(
'
SELECT * FROM users
'
);
console
.
log
(
'
item length
'
,
results
.
rows
.
length
);
console
.
log
(
'
item length
'
,
results
.
rows
.
length
);
const
temp
=
[];
for
(
let
i
=
0
;
i
<
results
.
rows
.
length
;
i
++
)
{
for
(
let
i
=
0
;
i
<
results
.
rows
.
length
;
i
++
)
{
const
element
=
results
.
rows
.
item
(
i
);
const
element
=
results
.
rows
.
item
(
i
);
temp
.
push
(
element
);
console
.
log
(
'
item
'
,
element
);
console
.
log
(
'
item
'
,
element
);
}
}
setUsers
(
temp
);
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
log
(
'
error in query user
'
,
error
);
console
.
log
(
'
error in query user
'
,
error
);
}
}
...
@@ -111,34 +73,40 @@ const App = () => {
...
@@ -111,34 +73,40 @@ const App = () => {
useEffect
(()
=>
{
useEffect
(()
=>
{
loadAndQueryDB
();
loadAndQueryDB
();
return
()
=>
{
return
()
=>
{
closeDatabase
(
db
);
//
closeDatabase(
await db); // 컴포넌트 없어질 때 디비 닫기 Error!!!
};
};
},
[]);
},
[]);
let
listViewItemSeparator
=
()
=>
{
return
(
<
View
style
=
{{
height
:
0.2
,
width
:
'
100%
'
,
backgroundColor
:
'
#808080
'
}}
/
>
);
};
let
listItemView
=
item
=>
{
console
.
log
(
'
item in list view
'
,
item
);
return
(
<
View
key
=
{
item
.
name
}
>
<
Text
>
Name
:
{
item
.
name
}
<
/Text
>
<
Text
>
Age
:
{
item
.
age
}
<
/Text
>
<
/View
>
);
};
console
.
log
(
'
users
'
,
users
);
return
(
return
(
<
SafeAreaView
>
<
SafeAreaView
>
<
Text
>
Hello
<
/Text
>
<
Text
>
Hello
<
/Text
>
<
View
style
=
{{
backgroundColor
:
'
red
'
}}
>
<
FlatList
data
=
{
users
}
ItemSeparatorComponent
=
{
listViewItemSeparator
}
keyExtractor
=
{(
item
,
index
)
=>
index
.
toString
()}
renderItem
=
{({
item
})
=>
listItemView
(
item
)}
/
>
<
/View
>
<
/SafeAreaView
>
<
/SafeAreaView
>
);
);
};
};
const
styles
=
StyleSheet
.
create
({
sectionContainer
:
{
marginTop
:
32
,
paddingHorizontal
:
24
,
},
sectionTitle
:
{
fontSize
:
24
,
fontWeight
:
'
600
'
,
},
sectionDescription
:
{
marginTop
:
8
,
fontSize
:
18
,
fontWeight
:
'
400
'
,
},
highlight
:
{
fontWeight
:
'
700
'
,
},
});
export
default
App
;
export
default
App
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment