1+ /*
2+ This creates two empty databases, populates values, and retrieves them back
3+ from the LITTLEFS file system
4+ */
5+ #include < stdio.h>
6+ #include < stdlib.h>
7+ #include < sqlite3.h>
8+ #include < SPI.h>
9+ #include < FS.h>
10+ #include " LittleFS.h"
11+
12+ /* You only need to format LITTLEFS the first time you run a
13+ test or else use the LITTLEFS plugin to create a partition
14+ https://github.com/lorol/arduino-esp32fs-plugin/releases */
15+ #define FORMAT_LITTLEFS_IF_FAILED true
16+
17+ const char * data = " Callback function called" ;
18+ static int callback (void *data, int argc, char **argv, char **azColName) {
19+ int i;
20+ Serial.printf (" %s: " , (const char *)data);
21+ for (i = 0 ; i<argc; i++){
22+ Serial.printf (" %s = %s\n " , azColName[i], argv[i] ? argv[i] : " NULL" );
23+ }
24+ Serial.printf (" \n " );
25+ return 0 ;
26+ }
27+
28+ int db_open (const char *filename, sqlite3 **db) {
29+ int rc = sqlite3_open (filename, db);
30+ if (rc) {
31+ Serial.printf (" Can't open database: %s\n " , sqlite3_errmsg (*db));
32+ return rc;
33+ } else {
34+ Serial.printf (" Opened database successfully\n " );
35+ }
36+ return rc;
37+ }
38+
39+ char *zErrMsg = 0 ;
40+ int db_exec (sqlite3 *db, const char *sql) {
41+ Serial.println (sql);
42+ long start = micros ();
43+ int rc = sqlite3_exec (db, sql, callback, (void *)data, &zErrMsg);
44+ if (rc != SQLITE_OK) {
45+ Serial.printf (" SQL error: %s\n " , zErrMsg);
46+ sqlite3_free (zErrMsg);
47+ } else {
48+ Serial.printf (" Operation done successfully\n " );
49+ }
50+ Serial.print (F (" Time taken:" ));
51+ Serial.println (micros ()-start);
52+ return rc;
53+ }
54+
55+ void setup () {
56+
57+ Serial.begin (115200 );
58+ sqlite3 *db1;
59+ sqlite3 *db2;
60+ int rc;
61+
62+ if (!LittleFS.begin (FORMAT_LITTLEFS_IF_FAILED, " /littlefs" )) {
63+ Serial.println (" Failed to mount file system" );
64+ return ;
65+ }
66+
67+ // list LITTLEFS contents
68+ File root = LittleFS.open (" /" );
69+ if (!root) {
70+ Serial.println (" - failed to open directory" );
71+ return ;
72+ }
73+ if (!root.isDirectory ()) {
74+ Serial.println (" - not a directory" );
75+ return ;
76+ }
77+ File file = root.openNextFile ();
78+ while (file) {
79+ if (file.isDirectory ()) {
80+ Serial.print (" DIR : " );
81+ Serial.println (file.name ());
82+ } else {
83+ Serial.print (" FILE: " );
84+ Serial.print (file.name ());
85+ Serial.print (" \t SIZE: " );
86+ Serial.println (file.size ());
87+ }
88+ file = root.openNextFile ();
89+ }
90+
91+ // remove existing file
92+ LittleFS.remove (" /test1.db" );
93+ LittleFS.remove (" /test2.db" );
94+
95+ sqlite3_initialize ();
96+
97+ if (db_open (" /littlefs/test1.db" , &db1))
98+ return ;
99+ if (db_open (" /littlefs/test2.db" , &db2))
100+ return ;
101+
102+ rc = db_exec (db1, " CREATE TABLE test1 (id INTEGER, content);" );
103+ if (rc != SQLITE_OK) {
104+ sqlite3_close (db1);
105+ sqlite3_close (db2);
106+ return ;
107+ }
108+ rc = db_exec (db2, " CREATE TABLE test2 (id INTEGER, content);" );
109+ if (rc != SQLITE_OK) {
110+ sqlite3_close (db1);
111+ sqlite3_close (db2);
112+ return ;
113+ }
114+
115+ rc = db_exec (db1, " INSERT INTO test1 VALUES (1, 'Hello, World from test1');" );
116+ if (rc != SQLITE_OK) {
117+ sqlite3_close (db1);
118+ sqlite3_close (db2);
119+ return ;
120+ }
121+ rc = db_exec (db2, " INSERT INTO test2 VALUES (1, 'Hello, World from test2');" );
122+ if (rc != SQLITE_OK) {
123+ sqlite3_close (db1);
124+ sqlite3_close (db2);
125+ return ;
126+ }
127+
128+ rc = db_exec (db1, " SELECT * FROM test1" );
129+ if (rc != SQLITE_OK) {
130+ sqlite3_close (db1);
131+ sqlite3_close (db2);
132+ return ;
133+ }
134+ rc = db_exec (db2, " SELECT * FROM test2" );
135+ if (rc != SQLITE_OK) {
136+ sqlite3_close (db1);
137+ sqlite3_close (db2);
138+ return ;
139+ }
140+
141+ sqlite3_close (db1);
142+ sqlite3_close (db2);
143+
144+ }
145+
146+ void loop () {
147+ }
0 commit comments