From 90b1000f467947a28c0ee47537f3d1dc424bf030 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 2 Aug 2024 00:51:42 +0100 Subject: [PATCH] added sqlite3 to android app --- capture/App.tsx | 240 +++++++++++++++++++++++++++++++++----- capture/package-lock.json | 18 ++- capture/package.json | 4 +- 3 files changed, 230 insertions(+), 32 deletions(-) diff --git a/capture/App.tsx b/capture/App.tsx index 1660dc9..870bd57 100644 --- a/capture/App.tsx +++ b/capture/App.tsx @@ -1,14 +1,188 @@ -import React, { useState } from 'react'; +import React, { useState, useEffect } from 'react'; import { TextInput, View, Text, TouchableOpacity, StyleSheet, PermissionsAndroid, Platform, Modal } from 'react-native'; import RNFS from 'react-native-fs'; import { launchCamera, CameraOptions } from 'react-native-image-picker'; import { request, PERMISSIONS, RESULTS } from 'react-native-permissions'; import { Calendar, DateData } from 'react-native-calendars'; +import SQLite, { SQLiteDatabase, Transaction, ResultSet } from 'react-native-sqlite-storage'; const App = () => { const [inputText, setInputText] = useState(''); const [isCalendarVisible, setIsCalendarVisible] = useState(false); const [selectedDate, setSelectedDate] = useState(''); + const [dbData, setDbData] = useState([]); + + const insertFakeData = (db: SQLiteDatabase) => { + db.transaction((tx: Transaction) => { + const unixTime = Math.floor(Date.now() / 1000); + tx.executeSql( + `INSERT INTO content (user_id, content_type, content_data, post_date, published) VALUES (?, ?, ?, ?, ?)`, + [1, 'post', 'testphone', unixTime, 0], + () => { + console.log('Fake data inserted successfully'); + }, + (error) => { + console.log('Error inserting fake data:', error); + } + ); + }); + }; + + + + // In the useEffect: + useEffect(() => { + const dbPath = `${RNFS.DocumentDirectoryPath}/database_default.sqlite3`; + console.log('Database path:', dbPath); + + const listDirectoryContents = async (path: string) => { + try { + const files = await RNFS.readDir(path); + console.log('Directory contents:', files); + } catch (error) { + console.error('Error reading directory:', error); + } + }; + + listDirectoryContents(RNFS.DocumentDirectoryPath); + + RNFS.exists(dbPath) + .then((exists) => { + if (exists) { + const db = SQLite.openDatabase( + { name: 'database_default.sqlite3', location: 'default' }, + () => { + console.log('Database opened'); + // insertFakeData(db); + fetchDbData(db); + }, + (error) => { + console.log('Error opening database:', error); + } + ); + } else { + const filePath = `${RNFS.DocumentDirectoryPath}/database_default.sqlite3`; + RNFS.writeFile(filePath, '', 'utf8') + .then(() => { + console.log('SQLite database file created:', filePath); + const db = SQLite.openDatabase( + { name: 'database_default.sqlite3', location: 'default' }, + () => { + console.log('New database created and opened'); + db.transaction((tx: Transaction) => { + createTables(tx); + }, (error: any) => { + console.log('Transaction error:', error); + }, () => { + console.log('Tables created successfully'); + // insertFakeData(db); + listDirectoryContents(RNFS.DocumentDirectoryPath); + }); + }, + (error) => { + console.log('Error creating new database:', error); + } + ); + }) + .catch((error) => { + console.error('Error creating SQLite database file:', error); + }); + } + }) + .catch((error) => { + console.log('Error checking if database exists:', error); + }); + }, []); + + + const createTables = (tx: Transaction) => { + tx.executeSql(` + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT + ); + `); + tx.executeSql(` + CREATE TABLE IF NOT EXISTS social_media_accounts ( + account_id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER, + platform_name TEXT, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + ); + `); + tx.executeSql(` + CREATE TABLE IF NOT EXISTS scheduler ( + scheduler_id INTEGER PRIMARY KEY AUTOINCREMENT, + content_id INTEGER, + social_media_account_id INTEGER, + scheduled_time DATETIME, + FOREIGN KEY (content_id) REFERENCES content(content_id) ON DELETE CASCADE, + FOREIGN KEY (social_media_account_id) REFERENCES social_media_accounts(account_id) ON DELETE CASCADE + ); + `); + tx.executeSql(` + CREATE TABLE IF NOT EXISTS content ( + content_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + user_id INTEGER NOT NULL, + content_type TEXT NOT NULL CHECK (content_type IN ('image', 'video', 'post')), + content_data TEXT NOT NULL, + post_date DATE NOT NULL, + description TEXT, + tags TEXT, + published INTEGER NOT NULL DEFAULT (0), + FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE + ); + `); + tx.executeSql(` + CREATE TABLE IF NOT EXISTS meta_accounts ( + ID INTEGER PRIMARY KEY AUTOINCREMENT, + account_id INTEGER, + meta_id TEXT, + meta_token TEXT, + account_name TEXT + ); + `); + tx.executeSql(` + CREATE TABLE IF NOT EXISTS twitter_accounts ( + ID INTEGER PRIMARY KEY AUTOINCREMENT, + account_id INTEGER, + twitter_consumer_key TEXT, + twitter_access_token TEXT, + twitter_access_token_secret TEXT, + account_name TEXT, + twitter_consumer_secret TEXT + ); + `); + tx.executeSql(` + CREATE TABLE IF NOT EXISTS linkedin_accounts ( + app_id TEXT, + account_id INTEGER PRIMARY KEY REFERENCES social_media_accounts (account_id), + app_secret TEXT, + app_token TEXT, + app_refresh_token TEXT, + app_token_expires_in INTEGER, + app_token_refresh_expires_in INTEGER, + account_name TEXT, + timestamp DATETIME + ); + `); + }; + + + const fetchDbData = (db: SQLiteDatabase) => { + db.transaction((tx: Transaction) => { + tx.executeSql('SELECT * FROM content', [], (tx: Transaction, results: ResultSet) => { + const rows = results.rows; + let data: any[] = []; + for (let i = 0; i < rows.length; i++) { + data.push(rows.item(i)); + } + console.log('Fetched data:', data); + setDbData(data); + }); + }); + }; + const requestPermissions = async () => { if (Platform.OS === 'android') { @@ -77,8 +251,6 @@ const App = () => { setIsCalendarVisible(true); }; - - const onDayPress = (day: DateData) => { setSelectedDate(day.dateString); setIsCalendarVisible(false); @@ -113,37 +285,41 @@ const App = () => { onChangeText={setInputText} /> -{/* Full screen Modal */} - setIsCalendarVisible(false)} -> - - - - - + setIsCalendarVisible(false)} + > + + + + Database Data: + {dbData.map((item, index) => ( + + {JSON.stringify(item)} + + ))} + ); }; - const styles = StyleSheet.create({ fullScreenModal: { flex: 1, @@ -196,6 +372,10 @@ const styles = StyleSheet.create({ alignItems: 'center', backgroundColor: 'rgba(0,0,0,0.5)', }, + dbText: { + color: 'white', + }, }); + export default App; diff --git a/capture/package-lock.json b/capture/package-lock.json index 493fd75..564a822 100644 --- a/capture/package-lock.json +++ b/capture/package-lock.json @@ -13,7 +13,8 @@ "react-native-calendars": "^1.1305.0", "react-native-camera": "^4.2.1", "react-native-fs": "^2.20.0", - "react-native-permissions": "^4.1.5" + "react-native-permissions": "^4.1.5", + "react-native-sqlite-storage": "^6.0.1" }, "devDependencies": { "@babel/core": "^7.20.0", @@ -24,6 +25,7 @@ "@react-native/metro-config": "0.74.83", "@react-native/typescript-config": "0.74.83", "@types/react": "^18.2.6", + "@types/react-native-sqlite-storage": "^6.0.5", "@types/react-test-renderer": "^18.0.0", "babel-jest": "^29.6.3", "eslint": "^8.19.0", @@ -4485,6 +4487,12 @@ "csstype": "^3.0.2" } }, + "node_modules/@types/react-native-sqlite-storage": { + "version": "6.0.5", + "resolved": "https://registry.npmjs.org/@types/react-native-sqlite-storage/-/react-native-sqlite-storage-6.0.5.tgz", + "integrity": "sha512-1gPwgYil6pHV/EDF816M8KmVsQC5nGMTAm8OMC25GbyuBaXFn7sdguMq6A2owtXB8c4qsH2tOEIQNetbOquawA==", + "dev": true + }, "node_modules/@types/react-test-renderer": { "version": "18.3.0", "resolved": "https://registry.npmjs.org/@types/react-test-renderer/-/react-test-renderer-18.3.0.tgz", @@ -12142,6 +12150,14 @@ } } }, + "node_modules/react-native-sqlite-storage": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/react-native-sqlite-storage/-/react-native-sqlite-storage-6.0.1.tgz", + "integrity": "sha512-1tDFjrint6X6qSYKf3gDyz+XB+X79jfiL6xTugKHPRtF0WvqMtVgdLuNqZunIXjNEvNtNVEbXaeZ6MsguFu00A==", + "peerDependencies": { + "react-native": ">=0.14.0" + } + }, "node_modules/react-native-swipe-gestures": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/react-native-swipe-gestures/-/react-native-swipe-gestures-1.0.5.tgz", diff --git a/capture/package.json b/capture/package.json index d9eada4..0554604 100644 --- a/capture/package.json +++ b/capture/package.json @@ -15,7 +15,8 @@ "react-native-calendars": "^1.1305.0", "react-native-camera": "^4.2.1", "react-native-fs": "^2.20.0", - "react-native-permissions": "^4.1.5" + "react-native-permissions": "^4.1.5", + "react-native-sqlite-storage": "^6.0.1" }, "devDependencies": { "@babel/core": "^7.20.0", @@ -26,6 +27,7 @@ "@react-native/metro-config": "0.74.83", "@react-native/typescript-config": "0.74.83", "@types/react": "^18.2.6", + "@types/react-native-sqlite-storage": "^6.0.5", "@types/react-test-renderer": "^18.0.0", "babel-jest": "^29.6.3", "eslint": "^8.19.0",