what are the usecases of expo-sqlite
- User
what are the usecases of expo-sqlite
Assistant
Here are the main use cases for Expo SQLite:
- Complex Data Relationships
// Example of related tables CREATE TABLE courses ( id INTEGER PRIMARY KEY, title TEXT, description TEXT ); CREATE TABLE lessons ( id INTEGER PRIMARY KEY, course_id INTEGER, title TEXT, content TEXT, FOREIGN KEY (course_id) REFERENCES courses (id) );- Large Datasets with Query Requirements
// Complex queries example const getFilteredLessons = async (searchTerm, categoryId, limit) => { const db = SQLite.openDatabase('myapp.db'); return new Promise((resolve) => { db.transaction(tx => { tx.executeSql( `SELECT * FROM lessons WHERE title LIKE ? AND category_id = ? ORDER BY created_at DESC LIMIT ?`, [`%${searchTerm}%`, categoryId, limit], (_, { rows: { _array } }) => resolve(_array) ); }); }); };- Data Aggregation and Statistics
// Example of aggregating user progress const getUserStats = async (userId) => { const db = SQLite.openDatabase('myapp.db'); return new Promise((resolve) => { db.transaction(tx => { tx.executeSql( `SELECT COUNT(*) as total_lessons, SUM(duration) as total_time, AVG(score) as avg_score FROM user_progress WHERE user_id = ?`, [userId], (_, { rows: { _array } }) => resolve(_array[0]) ); }); }); };- Offline Data Sync
// Example of tracking changes for sync const trackChange = async (entityType, entityId, operation) => { const db = SQLite.openDatabase('myapp.db'); return new Promise((resolve) => { db.transaction(tx => { tx.executeSql( `INSERT INTO sync_queue ( entity_type, entity_id, operation, timestamp ) VALUES (?, ?, ?, ?)`, [entityType, entityId, operation, Date.now()], (_, result) => resolve(result) ); }); }); };- Search Functionality
// Full-text search example const searchContent = async (query) => { const db = SQLite.openDatabase('myapp.db'); return new Promise((resolve) => { db.transaction(tx => { tx.executeSql( `SELECT * FROM lessons WHERE title LIKE ? OR content LIKE ? OR tags LIKE ?`, [`%${query}%`, `%${query}%`, `%${query}%`], (_, { rows: { _array } }) => resolve(_array) ); }); }); };- User Preferences and Settings
// Settings management const updateUserSettings = async (userId, settings) => { const db = SQLite.openDatabase('myapp.db'); return new Promise((resolve) => { db.transaction(tx => { tx.executeSql( `INSERT OR REPLACE INTO user_settings ( user_id, theme, notifications, language ) VALUES (?, ?, ?, ?)`, [userId, settings.theme, settings.notifications, settings.language], (_, result) => resolve(result) ); }); }); };- Progress Tracking
// Track user progress in courses const updateLessonProgress = async (userId, lessonId, progress) => { const db = SQLite.openDatabase('myapp.db'); return new Promise((resolve) => { db.transaction(tx => { tx.executeSql( `INSERT OR REPLACE INTO lesson_progress ( user_id, lesson_id, progress, last_position, completed ) VALUES (?, ?, ?, ?, ?)`, [userId, lessonId, progress, progress.position, progress.completed], (_, result) => resolve(result) ); }); }); };- Caching and Performance
// Cache frequently accessed data const cacheData = async (key, data, expiryTime) => { const db = SQLite.openDatabase('myapp.db'); return new Promise((resolve) => { db.transaction(tx => { tx.executeSql( `INSERT OR REPLACE INTO cache ( key, data, expiry ) VALUES (?, ?, ?)`, [key, JSON.stringify(data), Date.now() + expiryTime], (_, result) => resolve(result) ); }); }); };These use cases are particularly relevant when:
- You need to perform complex queries
- Your data has relationships between different entities
- You need to handle large amounts of structured data
- You need robust offline capabilities
- You need to perform data aggregation or analytics
- You need transaction support for data integrity
Would you like me to elaborate on any of these use cases or provide more specific examples for your application?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.