### Initialize SQLite Library Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Initializes the SQLite library and ESP32 VFS layer. Call this once in setup() after initializing the file system. ```cpp #include #include "SPIFFS.h" void setup() { Serial.begin(115200); // Initialize file system first if (!SPIFFS.begin(true)) { Serial.println("Failed to mount SPIFFS"); return; } // Initialize SQLite library sqlite3_initialize(); Serial.println("SQLite initialized successfully"); } ``` -------------------------------- ### sqlite3_initialize Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Initializes the SQLite library and sets up the ESP32 VFS layer. This function must be called once in setup() after the file system is initialized. ```APIDOC ## sqlite3_initialize ### Description Initializes the SQLite library. This function must be called before any other SQLite operations. It sets up the ESP32 VFS (Virtual File System) layer and registers the compression functions. Call this once in your `setup()` function after initializing the file system. ### Method (Not applicable, this is a library initialization function) ### Endpoint (Not applicable) ### Parameters (None) ### Request Example ```cpp #include #include "SPIFFS.h" void setup() { Serial.begin(115200); // Initialize file system first if (!SPIFFS.begin(true)) { Serial.println("Failed to mount SPIFFS"); return; } // Initialize SQLite library sqlite3_initialize(); Serial.println("SQLite initialized successfully"); } ``` ### Response (None) ### Response Example (None) ``` -------------------------------- ### Library Installation Paths Source: https://github.com/siara-cc/esp32_arduino_sqlite3_lib/blob/master/README.md Typical directory paths for the Arduino ESP32 libraries folder across different operating systems. ```text Windows: C:\Users\(username)\AppData\Roaming\Arduino15 Linux: /home//.arduino15 MacOS: /home//Library/Arduino15 ``` -------------------------------- ### Initialize SQLite3 with SD Card via SPI Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Uses the SPI interface to access database files stored on an SD card. Requires the /sd/ mount point prefix for file paths. ```cpp #include #include #include "SD.h" // SPI Wiring: // SD Card | ESP32 // DAT2 (1) - // DAT3 (2) SS (D5) // CMD (3) MOSI (D23) // VDD (4) 3.3V // CLK (5) SCK (D18) // VSS (6) GND // DAT0 (7) MISO (D19) // DAT1 (8) - sqlite3 *db; char *zErrMsg = 0; static int callback(void *data, int argc, char **argv, char **azColName) { for (int i = 0; i < argc; i++) { Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } Serial.println(); return 0; } void setup() { Serial.begin(115200); // Initialize SPI and SD card SPI.begin(); if (!SD.begin()) { Serial.println("SD card mount failed"); return; } sqlite3_initialize(); // Open database on SD card (SPI mount point) int rc = sqlite3_open("/sd/products.db", &db); if (rc) { Serial.printf("Can't open database: %s\n", sqlite3_errmsg(db)); return; } // Create and populate sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS items (sku TEXT, qty INTEGER);", NULL, NULL, NULL); sqlite3_exec(db, "INSERT INTO items VALUES ('ABC-123', 50);", NULL, NULL, NULL); Serial.println("Inventory:"); sqlite3_exec(db, "SELECT * FROM items;", callback, NULL, &zErrMsg); sqlite3_close(db); } ``` -------------------------------- ### Open SQLite Database Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Opens or creates a SQLite database file. The filename must include the mount point prefix. Returns SQLITE_OK on success. ```cpp #include #include "SPIFFS.h" sqlite3 *db; int openDatabase(const char *filename, sqlite3 **db) { int rc = sqlite3_open(filename, db); if (rc) { Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db)); return rc; } Serial.println("Opened database successfully"); return rc; } void setup() { Serial.begin(115200); SPIFFS.begin(true); sqlite3_initialize(); // Open database on SPIFFS if (openDatabase("/spiffs/mydata.db", &db)) { return; // Error opening database } // Database is now ready for queries // ... sqlite3_close(db); } ``` -------------------------------- ### Prepare and Execute SQL Statements with Parameter Binding Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Uses sqlite3_prepare_v2 and sqlite3_step to perform efficient database operations while preventing SQL injection. Requires calling sqlite3_clear_bindings and sqlite3_reset between repeated executions. ```cpp #include #include "SD_MMC.h" sqlite3 *db; sqlite3_stmt *stmt; const char *tail; void setup() { Serial.begin(115200); SD_MMC.begin(); sqlite3_initialize(); sqlite3_open("/sdcard/products.db", &db); // Create table sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS products (id INTEGER, name TEXT, price REAL);", NULL, NULL, NULL); // Prepare INSERT statement with parameters const char *sql = "INSERT INTO products VALUES (?, ?, ?);"; int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, &tail); if (rc != SQLITE_OK) { Serial.printf("Prepare error: %s\n", sqlite3_errmsg(db)); return; } // Insert multiple records using prepared statement for (int i = 1; i <= 5; i++) { char name[32]; snprintf(name, sizeof(name), "Product %d", i); sqlite3_bind_int(stmt, 1, i); // Bind id sqlite3_bind_text(stmt, 2, name, -1, SQLITE_TRANSIENT); // Bind name sqlite3_bind_double(stmt, 3, i * 9.99); // Bind price if (sqlite3_step(stmt) != SQLITE_DONE) { Serial.printf("Insert error: %s\n", sqlite3_errmsg(db)); } sqlite3_clear_bindings(stmt); sqlite3_reset(stmt); } sqlite3_finalize(stmt); // Query with prepared statement sql = "SELECT * FROM products WHERE price > ?;"; sqlite3_prepare_v2(db, sql, -1, &stmt, &tail); sqlite3_bind_double(stmt, 1, 20.0); Serial.println("Products with price > 20.0:"); while (sqlite3_step(stmt) == SQLITE_ROW) { int id = sqlite3_column_int(stmt, 0); const char *name = (const char*)sqlite3_column_text(stmt, 1); double price = sqlite3_column_double(stmt, 2); Serial.printf(" ID: %d, Name: %s, Price: %.2f\n", id, name, price); } sqlite3_finalize(stmt); sqlite3_close(db); } ``` -------------------------------- ### Initialize File Systems for SQLite Source: https://github.com/siara-cc/esp32_arduino_sqlite3_lib/blob/master/README.md Initialize the appropriate file system before calling SQLite C API functions. ```c++ SD_MMC.begin(); // for Cards attached to the High speed 4-bit port SPI.begin(); SD.begin(); // for Cards attached to the SPI bus SPIFFS.begin(); // For SPIFFS ``` -------------------------------- ### sqlite3_open Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Opens or creates a SQLite database file. The filename must include the mount point prefix (e.g., `/spiffs/`, `/littlefs/`, `/sd/`, or `/sdcard/`). ```APIDOC ## sqlite3_open ### Description Opens or creates a SQLite database file. The filename must include the mount point prefix (`/spiffs/`, `/littlefs/`, `/sd/`, or `/sdcard/`) followed by the database path. Returns `SQLITE_OK` (0) on success, or an error code on failure. ### Method (Not applicable, this is a library function) ### Endpoint (Not applicable) ### Parameters #### Path Parameters (None) #### Query Parameters (None) #### Request Body (None) ### Request Example ```cpp #include #include "SPIFFS.h" sqlite3 *db; int openDatabase(const char *filename, sqlite3 **db) { int rc = sqlite3_open(filename, db); if (rc) { Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db)); return rc; } Serial.println("Opened database successfully"); return rc; } void setup() { Serial.begin(115200); SPIFFS.begin(true); sqlite3_initialize(); // Open database on SPIFFS if (openDatabase("/spiffs/mydata.db", &db)) { return; // Error opening database } // Database is now ready for queries // ... sqlite3_close(db); } ``` ### Response #### Success Response (SQLITE_OK = 0) - **db** (sqlite3*) - Pointer to the opened database connection. #### Response Example (None) ``` -------------------------------- ### Manage Multiple SQLite3 Databases with LittleFS Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Utilizes LittleFS for flash storage, supporting multiple concurrent database connections. Files must be accessed using the /littlefs/ mount point prefix. ```cpp #include #include "LittleFS.h" #define FORMAT_LITTLEFS_IF_FAILED true sqlite3 *db1; sqlite3 *db2; char *zErrMsg = 0; static int callback(void *data, int argc, char **argv, char **azColName) { Serial.printf("%s: ", (const char*)data); for (int i = 0; i < argc; i++) { Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } return 0; } void setup() { Serial.begin(115200); // Initialize LittleFS with custom mount point if (!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED, "/littlefs")) { Serial.println("Failed to mount LittleFS"); return; } // List existing files File root = LittleFS.open("/"); File file = root.openNextFile(); while (file) { Serial.printf("File: %s, Size: %d\n", file.name(), file.size()); file = root.openNextFile(); } // Remove old databases LittleFS.remove("/test1.db"); LittleFS.remove("/test2.db"); sqlite3_initialize(); // Open multiple databases sqlite3_open("/littlefs/test1.db", &db1); sqlite3_open("/littlefs/test2.db", &db2); // Create tables in each database sqlite3_exec(db1, "CREATE TABLE test1 (id INTEGER, content TEXT);", NULL, NULL, NULL); sqlite3_exec(db2, "CREATE TABLE test2 (id INTEGER, content TEXT);", NULL, NULL, NULL); // Insert data sqlite3_exec(db1, "INSERT INTO test1 VALUES (1, 'Hello from DB1');", NULL, NULL, NULL); sqlite3_exec(db2, "INSERT INTO test2 VALUES (1, 'Hello from DB2');", NULL, NULL, NULL); // Query both databases sqlite3_exec(db1, "SELECT * FROM test1;", callback, (void*)"DB1", &zErrMsg); sqlite3_exec(db2, "SELECT * FROM test2;", callback, (void*)"DB2", &zErrMsg); sqlite3_close(db1); sqlite3_close(db2); } void loop() {} // Output: // DB1: id = 1 // content = Hello from DB1 // DB2: id = 1 // content = Hello from DB2 ``` -------------------------------- ### Compress and Decompress Text with Shox96 Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Uses shox96_0_2c() to store compressed blobs and shox96_0_2d() to retrieve original text from a SPIFFS-based database. ```cpp #include #include "SPIFFS.h" sqlite3 *db; char *zErrMsg = 0; static int callback(void *data, int argc, char **argv, char **azColName) { for (int i = 0; i < argc; i++) { Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } Serial.println(); return 0; } void setup() { Serial.begin(115200); SPIFFS.begin(true); sqlite3_initialize(); sqlite3_open("/spiffs/compressed.db", &db); // Create table with blob column for compressed data sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS logs (id INTEGER PRIMARY KEY, compressed_msg BLOB);", NULL, NULL, NULL); // Insert compressed text using shox96_0_2c() sqlite3_exec(db, "INSERT INTO logs (compressed_msg) VALUES (shox96_0_2c('Hello World'));", NULL, NULL, NULL); sqlite3_exec(db, "INSERT INTO logs (compressed_msg) VALUES (shox96_0_2c('Lorem Ipsum is simply dummy text of the printing and typesetting industry.'));", NULL, NULL, NULL); // Query original text and compressed size Serial.println("Compression comparison:"); sqlite3_exec(db, "SELECT shox96_0_2d(compressed_msg) AS original_text, " "length(shox96_0_2d(compressed_msg)) AS original_len, " "length(compressed_msg) AS compressed_len " "FROM logs;", callback, NULL, &zErrMsg); if (zErrMsg) { Serial.printf("Error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); } ``` -------------------------------- ### Expose SQLite Queries via ESP32 Web Server Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Uses chunked encoding to stream large result sets from an SQLite database to a web client. Requires SD_MMC storage and WiFi connectivity. ```cpp #include #include #include #include "SD_MMC.h" const char *ssid = "YourSSID"; const char *password = "YourPassword"; WebServer server(80); sqlite3 *db; sqlite3_stmt *res; const char *tail; int openDb(const char *filename) { int rc = sqlite3_open(filename, &db); if (rc) { Serial.printf("Can't open database: %s\n", sqlite3_errmsg(db)); } return rc; } void handleQuery() { String sql = "SELECT year, name, total_babies FROM names WHERE name BETWEEN '"; sql += server.arg("from"); sql += "' AND '"; sql += server.arg("to"); sql += "' LIMIT 100"; int rc = sqlite3_prepare_v2(db, sql.c_str(), -1, &res, &tail); if (rc != SQLITE_OK) { server.send(500, "text/plain", sqlite3_errmsg(db)); return; } // Use chunked encoding for streaming results server.setContentLength(CONTENT_LENGTH_UNKNOWN); server.send(200, "text/html", ""); while (sqlite3_step(res) == SQLITE_ROW) { String row = ""; server.sendContent(row); } server.sendContent("
YearNameCount
"; row += sqlite3_column_int(res, 0); row += ""; row += (const char*)sqlite3_column_text(res, 1); row += ""; row += sqlite3_column_int(res, 2); row += "
"); sqlite3_finalize(res); } void handleRoot() { String html = "

Database Query

"; html += "
"; html += "From:
"; html += "To:
"; html += ""; html += "
"; server.send(200, "text/html", html); } void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.printf("\nConnected! IP: %s\n", WiFi.localIP().toString().c_str()); SD_MMC.begin(); sqlite3_initialize(); openDb("/sdcard/babynames.db"); server.on("/", handleRoot); server.on("/query", handleQuery); server.begin(); } void loop() { server.handleClient(); } ``` -------------------------------- ### Default Mount Points Source: https://github.com/siara-cc/esp32_arduino_sqlite3_lib/blob/master/README.md The default mount paths used for file system access in SQLite functions. ```text '/sdcard' // for SD_MMC '/sd' // for SD on SPI '/spiffs' // For SPIFFS ``` -------------------------------- ### Compress and Decompress Text with Shox96 Source: https://github.com/siara-cc/esp32_arduino_sqlite3_lib/blob/master/README.md Use shox96_0_2c() to compress text data and shox96_0_2d() to decompress it. Ensure the input string is compatible with Shox96; otherwise, decompression may crash the program. This function is suitable for strings containing specific ASCII characters, CR, LF, and TAB. ```sql create table test (b1 blob); insert into test values (shox96_0_2c('Hello World')); insert into test values (shox96_0_2c('Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry''s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.')); select txt, length(txt) txt_len from (select shox96_0_2d(b1) txt from test); select length(b1) compressed_len from test; ``` -------------------------------- ### Execute SQL Statements with Callback Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Executes one or more SQL statements and processes results using a callback function. The callback receives column names and values for each row. ```cpp #include #include "SD_MMC.h" sqlite3 *db; char *zErrMsg = 0; // Callback function called for each row in result set static int callback(void *data, int argc, char **argv, char **azColName) { Serial.printf("%s:\n", (const char*)data); for (int i = 0; i < argc; i++) { Serial.printf(" %s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } return 0; } int executeSQL(sqlite3 *db, const char *sql) { Serial.println(sql); long start = micros(); int rc = sqlite3_exec(db, sql, callback, (void*)"Result", &zErrMsg); if (rc != SQLITE_OK) { Serial.printf("SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { Serial.println("Operation done successfully"); } Serial.printf("Time taken: %ld us\n", micros() - start); return rc; } void setup() { Serial.begin(115200); SD_MMC.begin(); sqlite3_initialize(); sqlite3_open("/sdcard/test.db", &db); // Create table executeSQL(db, "CREATE TABLE IF NOT EXISTS sensors (id INTEGER PRIMARY KEY, name TEXT, value REAL);"); // Insert data executeSQL(db, "INSERT INTO sensors (name, value) VALUES ('temperature', 23.5);"); executeSQL(db, "INSERT INTO sensors (name, value) VALUES ('humidity', 65.2);"); // Query data executeSQL(db, "SELECT * FROM sensors;"); sqlite3_close(db); } // Output: // Result: // id = 1 // name = temperature // value = 23.5 // Result: // id = 2 // name = humidity // value = 65.2 ``` -------------------------------- ### Retrieve Column Values with sqlite3_column_* Functions Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Demonstrates extracting various data types from a result set using specific column accessor functions. Ensure the correct function is used for the underlying SQL data type. ```cpp #include #include "SPIFFS.h" sqlite3 *db; sqlite3_stmt *stmt; const char *tail; void setup() { Serial.begin(115200); SPIFFS.begin(true); sqlite3_initialize(); sqlite3_open("/spiffs/data.db", &db); // Create and populate table with mixed data types sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS readings (timestamp INTEGER, sensor TEXT, value REAL, raw BLOB);", NULL, NULL, NULL); sqlite3_exec(db, "INSERT INTO readings VALUES (1699999999, 'temp_01', 25.5, X'DEADBEEF');", NULL, NULL, NULL); sqlite3_exec(db, "INSERT INTO readings VALUES (1700000000, 'hum_01', 62.3, X'CAFEBABE');", NULL, NULL, NULL); // Query and retrieve different column types const char *sql = "SELECT * FROM readings;"; sqlite3_prepare_v2(db, sql, -1, &stmt, &tail); Serial.println("Reading sensor data:"); while (sqlite3_step(stmt) == SQLITE_ROW) { // Get column count int colCount = sqlite3_column_count(stmt); // Retrieve integer column int timestamp = sqlite3_column_int(stmt, 0); // Retrieve text column const char *sensor = (const char*)sqlite3_column_text(stmt, 1); // Retrieve double column double value = sqlite3_column_double(stmt, 2); // Retrieve blob column const void *rawData = sqlite3_column_blob(stmt, 3); int blobSize = sqlite3_column_bytes(stmt, 3); // Get column name const char *colName = sqlite3_column_name(stmt, 2); Serial.printf("Timestamp: %d, Sensor: %s, %s: %.2f, Blob size: %d bytes\n", timestamp, sensor, colName, value, blobSize); } sqlite3_finalize(stmt); sqlite3_close(db); } ``` -------------------------------- ### SPI Bus Wiring Source: https://github.com/siara-cc/esp32_arduino_sqlite3_lib/blob/master/README.md Pin connections for attaching an SD card to the ESP32 via the SPI bus. ```text * SD Card | ESP32 * DAT2 (1) - * DAT3 (2) SS (D5) * CMD (3) MOSI (D23) * VDD (4) 3.3V * CLK (5) SCK (D19) * VSS (6) GND * DAT0 (7) MISO (D18) * DAT1 (8) - ``` -------------------------------- ### Access SQLite Database via SDMMC Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Configures the ESP32 to use the 4-bit SDMMC interface for high-speed database access on an SD card. ```cpp #include #include "SD_MMC.h" // SDMMC Wiring: // SD Card | ESP32 // DAT2 (1) D12 // DAT3 (2) D13 // CMD (3) D15 // VDD (4) 3.3V // CLK (5) D14 // VSS (6) GND // DAT0 (7) D2 // DAT1 (8) D4 sqlite3 *db; char *zErrMsg = 0; static int callback(void *data, int argc, char **argv, char **azColName) { for (int i = 0; i < argc; i++) { Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } Serial.println(); return 0; } void setup() { Serial.begin(115200); // Initialize SDMMC interface if (!SD_MMC.begin()) { Serial.println("SD_MMC mount failed"); return; } Serial.printf("SD Card Size: %lluMB\n", SD_MMC.cardSize() / (1024 * 1024)); sqlite3_initialize(); // Open database on SD card (SDMMC mount point) int rc = sqlite3_open("/sdcard/census2000names.db", &db); if (rc) { Serial.printf("Can't open database: %s\n", sqlite3_errmsg(db)); return; } Serial.println("Querying census database:"); sqlite3_exec(db, "SELECT * FROM surnames WHERE name = 'SMITH' LIMIT 5;", callback, NULL, &zErrMsg); sqlite3_close(db); } ``` -------------------------------- ### sqlite3_exec Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Executes one or more SQL statements and processes results using a callback function. ```APIDOC ## sqlite3_exec ### Description Executes one or more SQL statements. This is the simplest way to run SQL commands. It takes a callback function to process query results row by row. The callback receives column names and values for each row. ### Method (Not applicable, this is a library function) ### Endpoint (Not applicable) ### Parameters #### Path Parameters (None) #### Query Parameters (None) #### Request Body (None) ### Request Example ```cpp #include #include "SD_MMC.h" sqlite3 *db; char *zErrMsg = 0; // Callback function called for each row in result set static int callback(void *data, int argc, char **argv, char **azColName) { Serial.printf("%s:\n", (const char*)data); for (int i = 0; i < argc; i++) { Serial.printf(" %s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } return 0; } int executeSQL(sqlite3 *db, const char *sql) { Serial.println(sql); long start = micros(); int rc = sqlite3_exec(db, sql, callback, (void*)"Result", &zErrMsg); if (rc != SQLITE_OK) { Serial.printf("SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { Serial.println("Operation done successfully"); } Serial.printf("Time taken: %ld us\n", micros() - start); return rc; } void setup() { Serial.begin(115200); SD_MMC.begin(); sqlite3_initialize(); sqlite3_open("/sdcard/test.db", &db); // Create table executeSQL(db, "CREATE TABLE IF NOT EXISTS sensors (id INTEGER PRIMARY KEY, name TEXT, value REAL);"); // Insert data executeSQL(db, "INSERT INTO sensors (name, value) VALUES ('temperature', 23.5);"); executeSQL(db, "INSERT INTO sensors (name, value) VALUES ('humidity', 65.2);"); // Query data executeSQL(db, "SELECT * FROM sensors;"); sqlite3_close(db); } // Output: // Result: // id = 1 // name = temperature // value = 23.5 // Result: // id = 2 // name = humidity // value = 65.2 ``` ### Response #### Success Response (SQLITE_OK = 0) - **rc** (int) - Returns `SQLITE_OK` (0) on success, or an error code on failure. #### Response Example (None) ``` -------------------------------- ### Compress and Decompress Unicode with Unishox Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Uses unishox1c() and unishox1d() to handle UTF-8 encoded text within SQLite database operations. ```cpp #include #include "SPIFFS.h" sqlite3 *db; char *zErrMsg = 0; static int callback(void *data, int argc, char **argv, char **azColName) { for (int i = 0; i < argc; i++) { Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } return 0; } void setup() { Serial.begin(115200); SPIFFS.begin(true); sqlite3_initialize(); sqlite3_open("/spiffs/unicode.db", &db); // Create table for Unicode messages sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, data BLOB);", NULL, NULL, NULL); // Insert UTF-8 text compressed with unishox1c() sqlite3_exec(db, "INSERT INTO messages (data) VALUES (unishox1c('Temperature sensor reading: 25.5C'));", NULL, NULL, NULL); // Decompress and display using unishox1d() Serial.println("Decompressed message:"); sqlite3_exec(db, "SELECT unishox1d(data) AS message FROM messages;", callback, NULL, &zErrMsg); sqlite3_close(db); } ``` -------------------------------- ### Handle SQLite Errors Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Retrieves human-readable error messages and extended error codes. Remember to free the error message buffer using sqlite3_free. ```cpp #include #include "SPIFFS.h" sqlite3 *db; char *zErrMsg = 0; void setup() { Serial.begin(115200); SPIFFS.begin(true); sqlite3_initialize(); // Try to open database int rc = sqlite3_open("/spiffs/test.db", &db); if (rc != SQLITE_OK) { Serial.printf("Open error [%d]: %s\n", rc, sqlite3_errmsg(db)); return; } // Execute invalid SQL to demonstrate error handling rc = sqlite3_exec(db, "SELECT * FROM nonexistent_table;", NULL, NULL, &zErrMsg); if (rc != SQLITE_OK) { Serial.printf("SQL error code: %d\n", rc); Serial.printf("Extended error: %d\n", sqlite3_extended_errcode(db)); Serial.printf("Error message: %s\n", zErrMsg); sqlite3_free(zErrMsg); // Always free error messages } // Try to create table with syntax error rc = sqlite3_exec(db, "CREATE TABL test (id INTEGER);", NULL, NULL, &zErrMsg); if (rc != SQLITE_OK) { Serial.printf("Syntax error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } sqlite3_close(db); } void loop() {} // Output: // SQL error code: 1 // Extended error: 1 // Error message: no such table: nonexistent_table // Syntax error: near "TABL": syntax error ``` -------------------------------- ### Close SQLite Database Connection Source: https://context7.com/siara-cc/esp32_arduino_sqlite3_lib/llms.txt Ensures all pending writes are flushed to storage by closing the database connection. Always call this when finished with database operations. ```cpp #include #include "SPIFFS.h" sqlite3 *db; void setup() { Serial.begin(115200); SPIFFS.begin(true); sqlite3_initialize(); // Open database int rc = sqlite3_open("/spiffs/temp.db", &db); if (rc != SQLITE_OK) { Serial.printf("Error opening: %s\n", sqlite3_errmsg(db)); return; } // Perform operations... sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS test (id INTEGER);", NULL, NULL, NULL); sqlite3_exec(db, "INSERT INTO test VALUES (1), (2), (3);", NULL, NULL, NULL); // Close database - flushes pending writes rc = sqlite3_close(db); if (rc == SQLITE_OK) { Serial.println("Database closed successfully"); } else { Serial.printf("Error closing: %s\n", sqlite3_errmsg(db)); } } void loop() {} ``` -------------------------------- ### SD_MMC Port Wiring Source: https://github.com/siara-cc/esp32_arduino_sqlite3_lib/blob/master/README.md Pin connections for attaching an SD card to the ESP32 via the high-speed 4-bit SD_MMC port. ```text * SD Card | ESP32 * DAT2 (1) D12 * DAT3 (2) D13 * CMD (3) D15 * VDD (4) 3.3V * CLK (5) D14 * VSS (6) GND * DAT0 (7) D2 * DAT1 (8) D4 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.