|
| 1 | +/* Example to create a table and insert 5000 bytes into blob column. |
| 2 | + Creates the database on SD Card connected to the SDMMC port. |
| 3 | +*/ |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <sqlite3.h> |
| 7 | +#include <SPI.h> |
| 8 | +#include <FS.h> |
| 9 | +#include "SD_MMC.h" |
| 10 | + |
| 11 | +const char* data = "Callback function called"; |
| 12 | +static int callback(void *data, int argc, char **argv, char **azColName){ |
| 13 | + int i; |
| 14 | + Serial.printf("%s: ", (const char*)data); |
| 15 | + for (i = 0; i<argc; i++){ |
| 16 | + Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
| 17 | + } |
| 18 | + Serial.printf("\n"); |
| 19 | + return 0; |
| 20 | +} |
| 21 | + |
| 22 | +int openDb(const char *filename, sqlite3 **db) { |
| 23 | + int rc = sqlite3_open(filename, db); |
| 24 | + if (rc) { |
| 25 | + Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db)); |
| 26 | + return rc; |
| 27 | + } else { |
| 28 | + Serial.printf("Opened database successfully\n"); |
| 29 | + } |
| 30 | + return rc; |
| 31 | +} |
| 32 | + |
| 33 | +char *zErrMsg = 0; |
| 34 | +int db_exec(sqlite3 *db, const char *sql) { |
| 35 | + Serial.println(sql); |
| 36 | + long start = micros(); |
| 37 | + int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg); |
| 38 | + if (rc != SQLITE_OK) { |
| 39 | + Serial.printf("SQL error: %s\n", zErrMsg); |
| 40 | + sqlite3_free(zErrMsg); |
| 41 | + } else { |
| 42 | + Serial.printf("Operation done successfully\n"); |
| 43 | + } |
| 44 | + Serial.print(F("Time taken:")); |
| 45 | + Serial.println(micros()-start); |
| 46 | + return rc; |
| 47 | +} |
| 48 | + |
| 49 | +#define DATA_SIZE 5000 |
| 50 | + |
| 51 | +void setup() { |
| 52 | + Serial.begin(115200); |
| 53 | + sqlite3 *db1; |
| 54 | + sqlite3_stmt *res; |
| 55 | + const char *tail; |
| 56 | + int rc; |
| 57 | + |
| 58 | + char *data = (char *) malloc(DATA_SIZE); |
| 59 | + |
| 60 | + SPI.begin(); |
| 61 | + SD_MMC.begin(); |
| 62 | + |
| 63 | + sqlite3_initialize(); |
| 64 | + |
| 65 | + // Open database 1 |
| 66 | + if (openDb("/sdcard/test_long_data.db", &db1)) |
| 67 | + return; |
| 68 | + |
| 69 | + rc = db_exec(db1, "CREATE TABLE IF NOT EXISTS test (c1 blob)"); |
| 70 | + if (rc != SQLITE_OK) { |
| 71 | + sqlite3_close(db1); |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + for (int i = 0; i < DATA_SIZE; i++) { |
| 76 | + data[i] = 'a' + i % 26; |
| 77 | + } |
| 78 | + const char *sql = "INSERT INTO test VALUES (?)"; |
| 79 | + rc = sqlite3_prepare_v2(db1, sql, strlen(sql), &res, &tail); |
| 80 | + if (rc != SQLITE_OK) { |
| 81 | + Serial.printf("ERROR preparing sql: %s\n", sqlite3_errmsg(db1)); |
| 82 | + sqlite3_close(db1); |
| 83 | + return; |
| 84 | + } |
| 85 | + sqlite3_bind_blob(res, 1, data, DATA_SIZE, SQLITE_STATIC); |
| 86 | + if (sqlite3_step(res) != SQLITE_DONE) { |
| 87 | + Serial.printf("ERROR inserting data: %s\n", sqlite3_errmsg(db1)); |
| 88 | + sqlite3_close(db1); |
| 89 | + return; |
| 90 | + } |
| 91 | + sqlite3_finalize(res); |
| 92 | + |
| 93 | + rc = db_exec(db1, "Select length(c1), substr(c1, 1, 10) from test"); |
| 94 | + if (rc != SQLITE_OK) { |
| 95 | + sqlite3_close(db1); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + sqlite3_close(db1); |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +void loop() { |
| 104 | +} |
| 105 | + |
0 commit comments