Skip to content

Commit 90b1000

Browse files
committed
added sqlite3 to android app
1 parent 14de673 commit 90b1000

File tree

3 files changed

+230
-32
lines changed

3 files changed

+230
-32
lines changed

capture/App.tsx

Lines changed: 210 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,188 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import { TextInput, View, Text, TouchableOpacity, StyleSheet, PermissionsAndroid, Platform, Modal } from 'react-native';
33
import RNFS from 'react-native-fs';
44
import { launchCamera, CameraOptions } from 'react-native-image-picker';
55
import { request, PERMISSIONS, RESULTS } from 'react-native-permissions';
66
import { Calendar, DateData } from 'react-native-calendars';
7+
import SQLite, { SQLiteDatabase, Transaction, ResultSet } from 'react-native-sqlite-storage';
78

89
const App = () => {
910
const [inputText, setInputText] = useState('');
1011
const [isCalendarVisible, setIsCalendarVisible] = useState(false);
1112
const [selectedDate, setSelectedDate] = useState('');
13+
const [dbData, setDbData] = useState<any[]>([]);
14+
15+
const insertFakeData = (db: SQLiteDatabase) => {
16+
db.transaction((tx: Transaction) => {
17+
const unixTime = Math.floor(Date.now() / 1000);
18+
tx.executeSql(
19+
`INSERT INTO content (user_id, content_type, content_data, post_date, published) VALUES (?, ?, ?, ?, ?)`,
20+
[1, 'post', 'testphone', unixTime, 0],
21+
() => {
22+
console.log('Fake data inserted successfully');
23+
},
24+
(error) => {
25+
console.log('Error inserting fake data:', error);
26+
}
27+
);
28+
});
29+
};
30+
31+
32+
33+
// In the useEffect:
34+
useEffect(() => {
35+
const dbPath = `${RNFS.DocumentDirectoryPath}/database_default.sqlite3`;
36+
console.log('Database path:', dbPath);
37+
38+
const listDirectoryContents = async (path: string) => {
39+
try {
40+
const files = await RNFS.readDir(path);
41+
console.log('Directory contents:', files);
42+
} catch (error) {
43+
console.error('Error reading directory:', error);
44+
}
45+
};
46+
47+
listDirectoryContents(RNFS.DocumentDirectoryPath);
48+
49+
RNFS.exists(dbPath)
50+
.then((exists) => {
51+
if (exists) {
52+
const db = SQLite.openDatabase(
53+
{ name: 'database_default.sqlite3', location: 'default' },
54+
() => {
55+
console.log('Database opened');
56+
// insertFakeData(db);
57+
fetchDbData(db);
58+
},
59+
(error) => {
60+
console.log('Error opening database:', error);
61+
}
62+
);
63+
} else {
64+
const filePath = `${RNFS.DocumentDirectoryPath}/database_default.sqlite3`;
65+
RNFS.writeFile(filePath, '', 'utf8')
66+
.then(() => {
67+
console.log('SQLite database file created:', filePath);
68+
const db = SQLite.openDatabase(
69+
{ name: 'database_default.sqlite3', location: 'default' },
70+
() => {
71+
console.log('New database created and opened');
72+
db.transaction((tx: Transaction) => {
73+
createTables(tx);
74+
}, (error: any) => {
75+
console.log('Transaction error:', error);
76+
}, () => {
77+
console.log('Tables created successfully');
78+
// insertFakeData(db);
79+
listDirectoryContents(RNFS.DocumentDirectoryPath);
80+
});
81+
},
82+
(error) => {
83+
console.log('Error creating new database:', error);
84+
}
85+
);
86+
})
87+
.catch((error) => {
88+
console.error('Error creating SQLite database file:', error);
89+
});
90+
}
91+
})
92+
.catch((error) => {
93+
console.log('Error checking if database exists:', error);
94+
});
95+
}, []);
96+
97+
98+
const createTables = (tx: Transaction) => {
99+
tx.executeSql(`
100+
CREATE TABLE IF NOT EXISTS users (
101+
id INTEGER PRIMARY KEY AUTOINCREMENT,
102+
name TEXT
103+
);
104+
`);
105+
tx.executeSql(`
106+
CREATE TABLE IF NOT EXISTS social_media_accounts (
107+
account_id INTEGER PRIMARY KEY AUTOINCREMENT,
108+
user_id INTEGER,
109+
platform_name TEXT,
110+
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
111+
);
112+
`);
113+
tx.executeSql(`
114+
CREATE TABLE IF NOT EXISTS scheduler (
115+
scheduler_id INTEGER PRIMARY KEY AUTOINCREMENT,
116+
content_id INTEGER,
117+
social_media_account_id INTEGER,
118+
scheduled_time DATETIME,
119+
FOREIGN KEY (content_id) REFERENCES content(content_id) ON DELETE CASCADE,
120+
FOREIGN KEY (social_media_account_id) REFERENCES social_media_accounts(account_id) ON DELETE CASCADE
121+
);
122+
`);
123+
tx.executeSql(`
124+
CREATE TABLE IF NOT EXISTS content (
125+
content_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
126+
user_id INTEGER NOT NULL,
127+
content_type TEXT NOT NULL CHECK (content_type IN ('image', 'video', 'post')),
128+
content_data TEXT NOT NULL,
129+
post_date DATE NOT NULL,
130+
description TEXT,
131+
tags TEXT,
132+
published INTEGER NOT NULL DEFAULT (0),
133+
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
134+
);
135+
`);
136+
tx.executeSql(`
137+
CREATE TABLE IF NOT EXISTS meta_accounts (
138+
ID INTEGER PRIMARY KEY AUTOINCREMENT,
139+
account_id INTEGER,
140+
meta_id TEXT,
141+
meta_token TEXT,
142+
account_name TEXT
143+
);
144+
`);
145+
tx.executeSql(`
146+
CREATE TABLE IF NOT EXISTS twitter_accounts (
147+
ID INTEGER PRIMARY KEY AUTOINCREMENT,
148+
account_id INTEGER,
149+
twitter_consumer_key TEXT,
150+
twitter_access_token TEXT,
151+
twitter_access_token_secret TEXT,
152+
account_name TEXT,
153+
twitter_consumer_secret TEXT
154+
);
155+
`);
156+
tx.executeSql(`
157+
CREATE TABLE IF NOT EXISTS linkedin_accounts (
158+
app_id TEXT,
159+
account_id INTEGER PRIMARY KEY REFERENCES social_media_accounts (account_id),
160+
app_secret TEXT,
161+
app_token TEXT,
162+
app_refresh_token TEXT,
163+
app_token_expires_in INTEGER,
164+
app_token_refresh_expires_in INTEGER,
165+
account_name TEXT,
166+
timestamp DATETIME
167+
);
168+
`);
169+
};
170+
171+
172+
const fetchDbData = (db: SQLiteDatabase) => {
173+
db.transaction((tx: Transaction) => {
174+
tx.executeSql('SELECT * FROM content', [], (tx: Transaction, results: ResultSet) => {
175+
const rows = results.rows;
176+
let data: any[] = [];
177+
for (let i = 0; i < rows.length; i++) {
178+
data.push(rows.item(i));
179+
}
180+
console.log('Fetched data:', data);
181+
setDbData(data);
182+
});
183+
});
184+
};
185+
12186

13187
const requestPermissions = async () => {
14188
if (Platform.OS === 'android') {
@@ -77,8 +251,6 @@ const App = () => {
77251
setIsCalendarVisible(true);
78252
};
79253

80-
81-
82254
const onDayPress = (day: DateData) => {
83255
setSelectedDate(day.dateString);
84256
setIsCalendarVisible(false);
@@ -113,37 +285,41 @@ const App = () => {
113285
onChangeText={setInputText}
114286
/>
115287

116-
{/* Full screen Modal */}
117-
<Modal presentationStyle='fullScreen'
118-
visible={isCalendarVisible}
119-
animationType='slide'
120-
onRequestClose={() => setIsCalendarVisible(false)}
121-
>
122-
123-
<Calendar
124-
onDayPress={onDayPress}
125-
markedDates={{
126-
[selectedDate]: { selected: true, marked: true, selectedColor: 'blue' },
127-
}}
128-
theme={{
129-
'stylesheet.calendar.main': {
130-
base: {
131-
width: '100%',
132-
height: '100%',
133-
justifyContent: 'center',
134-
alignItems: 'center',
135-
},
136-
},
137-
}}
138-
/>
139-
140-
141-
</Modal>
288+
<Modal
289+
presentationStyle="fullScreen"
290+
visible={isCalendarVisible}
291+
animationType="slide"
292+
onRequestClose={() => setIsCalendarVisible(false)}
293+
>
294+
<Calendar
295+
onDayPress={onDayPress}
296+
markedDates={{
297+
[selectedDate]: { selected: true, marked: true, selectedColor: 'blue' },
298+
}}
299+
theme={{
300+
'stylesheet.calendar.main': {
301+
base: {
302+
width: '100%',
303+
height: '100%',
304+
justifyContent: 'center',
305+
alignItems: 'center',
306+
},
307+
},
308+
}}
309+
/>
310+
</Modal>
142311

312+
<View>
313+
<Text style={styles.title}>Database Data:</Text>
314+
{dbData.map((item, index) => (
315+
<Text key={index} style={styles.dbText}>
316+
{JSON.stringify(item)}
317+
</Text>
318+
))}
319+
</View>
143320
</View>
144321
);
145322
};
146-
147323
const styles = StyleSheet.create({
148324
fullScreenModal: {
149325
flex: 1,
@@ -196,6 +372,10 @@ const styles = StyleSheet.create({
196372
alignItems: 'center',
197373
backgroundColor: 'rgba(0,0,0,0.5)',
198374
},
375+
dbText: {
376+
color: 'white',
377+
},
199378
});
200379

380+
201381
export default App;

capture/package-lock.json

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

capture/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"react-native-calendars": "^1.1305.0",
1616
"react-native-camera": "^4.2.1",
1717
"react-native-fs": "^2.20.0",
18-
"react-native-permissions": "^4.1.5"
18+
"react-native-permissions": "^4.1.5",
19+
"react-native-sqlite-storage": "^6.0.1"
1920
},
2021
"devDependencies": {
2122
"@babel/core": "^7.20.0",
@@ -26,6 +27,7 @@
2627
"@react-native/metro-config": "0.74.83",
2728
"@react-native/typescript-config": "0.74.83",
2829
"@types/react": "^18.2.6",
30+
"@types/react-native-sqlite-storage": "^6.0.5",
2931
"@types/react-test-renderer": "^18.0.0",
3032
"babel-jest": "^29.6.3",
3133
"eslint": "^8.19.0",

0 commit comments

Comments
 (0)