|
| 1 | +# BrightSign SQLite Database Example |
| 2 | + |
| 3 | +This example demonstrates how to use SQLite database functionality in a BrightSign application, showing the communication between BrightScript and JavaScript for database operations. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The example showcases: |
| 8 | +- Creating a SQLite database |
| 9 | +- Creating tables with SQL commands |
| 10 | +- Inserting records into the database |
| 11 | +- Querying records from the database |
| 12 | +- Deleting records from the database |
| 13 | +- Proper database cleanup and connection management |
| 14 | + |
| 15 | +## Files |
| 16 | + |
| 17 | +- `autorun.brs` - BrightScript application that handles database operations |
| 18 | +- `index.js` - JavaScript application that sends SQL commands via MessagePort |
| 19 | +- `README.md` - This documentation file |
| 20 | + |
| 21 | +## How It Works |
| 22 | + |
| 23 | +1. **Initialization**: The BrightScript application (`autorun.brs`) starts and creates a Node.js instance running `index.js` |
| 24 | +2. **Database Creation**: When the JavaScript app signals it's ready, BrightScript creates a SQLite database at `SD:/example.db` |
| 25 | +3. **Table Creation**: JavaScript sends a command to create a `users` table with columns for ID, name, and age |
| 26 | +4. **Data Insertion**: Sample user records are inserted into the database |
| 27 | +5. **Data Retrieval**: All records are queried from the database |
| 28 | +6. **Data Deletion**: Each retrieved record is deleted from the database |
| 29 | +7. **Cleanup**: The database connection is properly closed when the application exits |
| 30 | + |
| 31 | +## Database Schema |
| 32 | + |
| 33 | +The example creates a simple `users` table: |
| 34 | + |
| 35 | +```sql |
| 36 | +CREATE TABLE IF NOT EXISTS users ( |
| 37 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 38 | + name TEXT, |
| 39 | + age INTEGER |
| 40 | +); |
| 41 | +``` |
| 42 | + |
| 43 | +## Sample Data |
| 44 | + |
| 45 | +The example inserts the following sample records: |
| 46 | +- Alice, age 25 |
| 47 | +- Bob, age 30 |
| 48 | +- Charlie, age 35 |
| 49 | + |
| 50 | +## Key Features |
| 51 | + |
| 52 | +### BrightScript Side (`autorun.brs`) |
| 53 | +- **Database Management**: Creates and manages SQLite database connection |
| 54 | +- **SQL Statement Execution**: Handles CREATE, INSERT, SELECT, and DELETE operations |
| 55 | +- **Result Processing**: Processes query results and formats them for JavaScript consumption |
| 56 | +- **Error Handling**: Includes error checking for database operations |
| 57 | +- **Resource Cleanup**: Properly closes database connections on exit |
| 58 | + |
| 59 | +### JavaScript Side (`index.js`) |
| 60 | +- **MessagePort Communication**: Uses `@brightsign/messageport` for BrightScript communication |
| 61 | +- **Command Orchestration**: Sends SQL commands in logical sequence |
| 62 | +- **Data Processing**: Parses and processes database results |
| 63 | +- **Event-Driven Architecture**: Responds to database operation completion events |
| 64 | + |
| 65 | +## Running the Example |
| 66 | + |
| 67 | +1. Copy the `autorun.brs` and `index.js` files to the root of your BrightSign player's SD card |
| 68 | +2. Power on or restart your BrightSign player |
| 69 | +3. The application will automatically start and demonstrate the database operations |
| 70 | +4. Check the device logs to see the SQL operations being performed |
| 71 | + |
| 72 | +## Expected Output |
| 73 | + |
| 74 | +The application will log information about each database operation: |
| 75 | +- Database creation confirmation |
| 76 | +- Table creation success |
| 77 | +- Record insertion confirmations |
| 78 | +- Retrieved records display |
| 79 | +- Record deletion confirmations |
| 80 | + |
| 81 | +## Notes |
| 82 | + |
| 83 | +- The database file is created at `SD:/example.db` |
| 84 | +- The application demonstrates a complete CRUD (Create, Read, Update, Delete) cycle |
| 85 | +- Error handling is included for robust database operations |
| 86 | +- The database connection is automatically closed when the Nodejs application exits |
| 87 | +- Results from SELECT queries are formatted as JSON strings for JavaScript processing |
| 88 | + |
| 89 | +This example serves as a foundation for building more complex database-driven BrightSign applications. |
0 commit comments