Skip to content

Commit 8a326d7

Browse files
committed
new file: ESP32_MSD_Shield_Wiring.jpg
modified: README.md new file: examples/sqlite3_sdspi/sqlite3_sdspi.ino Include example and picture for SPI mode SD Card access
1 parent 8b59364 commit 8a326d7

File tree

3 files changed

+137
-19
lines changed

3 files changed

+137
-19
lines changed

ESP32_MSD_Shield_Wiring.jpg

128 KB
Loading

README.md

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Sqlite3 Arduino library for ESP32
2-
This library enables access to SQLite database files from SPIFFS or SD Cards through ESP32 SoC. Given below is a picture of a board that has a ready-made Micro SD slot:
2+
3+
This library enables access to SQLite database files from SPIFFS or SD Cards through ESP32 SoC. Given below is a picture of a board that has a ready-made Micro SD slot (using SDMMC 4 bit mode - see example sqlite3_sdmmc):
34

45
![](ESP_WROOM_32_breakout.png?raw=true)
56

7+
Also shown below is the wiring between ESP-WROOM-32 breakout board and Micro SD Shield (using SPI mode - see example sqlite3_sdspi):
8+
9+
![](ESP32_MSD_Shield_Wiring.jpg?raw=true)
10+
611
## Usage
12+
713
Sqlite3 C API such as `sqlite3_open` can be directly invoked. Before calling please invoke:
814

915
```c++
@@ -31,29 +37,29 @@ Please see the examples for full illustration of usage for the different file sy
3137
While there is no wiring needed for SPIFFS, for attaching cards to SPI bus, please use the following connections:
3238

3339
```c++
34-
* SD Card | ESP32
35-
* DAT2 -
36-
* DAT3 SS
37-
* CMD MOSI
38-
* VSS GND
39-
* VDD 3.3V
40-
* CLK SCK
41-
* DAT0 MISO
42-
* DAT1 -
40+
* SD Card | ESP32
41+
* DAT2 (1) -
42+
* DAT3 (2) SS (D5)
43+
* CMD (3) MOSI (D23)
44+
* VDD (4) 3.3V
45+
* CLK (5) SCK (D19)
46+
* VSS (6) GND
47+
* DAT0 (7) MISO (D18)
48+
* DAT1 (8) -
4349
```
4450
4551
And for SD card attached to High-speed 4-bit SD_MMC port, use:
4652
4753
```c++
48-
* SD Card | ESP32
49-
* DAT2 12
50-
* DAT3 13
51-
* CMD 15
52-
* VSS GND
53-
* VDD 3.3V
54-
* CLK 14
55-
* DAT0 2 (add 1K pull up after flashing)
56-
* DAT1 4
54+
* SD Card | ESP32
55+
* DAT2 (1) D12
56+
* DAT3 (2) D13
57+
* CMD (3) D15
58+
* VDD (4) 3.3V
59+
* CLK (5) D14
60+
* VSS (6) GND
61+
* DAT0 (7) D2
62+
* DAT1 (8) D4
5763
```
5864

5965
If you are using a board such as shown in the picture above, this wiring is ready-made.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
This example opens Sqlite3 databases from SD Card and
3+
retrieves data from them.
4+
Before running please copy following files to SD Card:
5+
examples/sqlite3_sdmmc/data/mdr512.db
6+
examples/sqlite3_sdmmc/data/census2000names.db
7+
Connections:
8+
* SD Card | ESP32
9+
* DAT2 -
10+
* DAT3 SS (D5)
11+
* CMD MOSI (D23)
12+
* VSS GND
13+
* VDD 3.3V
14+
* CLK SCK (D18)
15+
* DAT0 MISO (D19)
16+
* DAT1 -
17+
*/
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <sqlite3.h>
21+
#include <SPI.h>
22+
#include <FS.h>
23+
#include "SD.h"
24+
25+
const char* data = "Callback function called";
26+
static int callback(void *data, int argc, char **argv, char **azColName){
27+
int i;
28+
Serial.printf("%s: ", (const char*)data);
29+
for (i = 0; i<argc; i++){
30+
Serial.printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
31+
}
32+
Serial.printf("\n");
33+
return 0;
34+
}
35+
36+
int openDb(const char *filename, sqlite3 **db) {
37+
int rc = sqlite3_open(filename, db);
38+
if (rc) {
39+
Serial.printf("Can't open database: %s\n", sqlite3_errmsg(*db));
40+
return rc;
41+
} else {
42+
Serial.printf("Opened database successfully\n");
43+
}
44+
return rc;
45+
}
46+
47+
char *zErrMsg = 0;
48+
int db_exec(sqlite3 *db, const char *sql) {
49+
Serial.println(sql);
50+
long start = micros();
51+
int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
52+
if (rc != SQLITE_OK) {
53+
Serial.printf("SQL error: %s\n", zErrMsg);
54+
sqlite3_free(zErrMsg);
55+
} else {
56+
Serial.printf("Operation done successfully\n");
57+
}
58+
Serial.print(F("Time taken:"));
59+
Serial.println(micros()-start);
60+
return rc;
61+
}
62+
63+
void setup() {
64+
Serial.begin(115200);
65+
sqlite3 *db1;
66+
sqlite3 *db2;
67+
char *zErrMsg = 0;
68+
int rc;
69+
70+
SPI.begin();
71+
SD.begin();
72+
73+
sqlite3_initialize();
74+
75+
// Open database 1
76+
if (openDb("/sd/census2000names.db", &db1))
77+
return;
78+
if (openDb("/sd/mdr512.db", &db2))
79+
return;
80+
81+
rc = db_exec(db1, "Select * from surnames where name = 'MICHELLE'");
82+
if (rc != SQLITE_OK) {
83+
sqlite3_close(db1);
84+
sqlite3_close(db2);
85+
return;
86+
}
87+
rc = db_exec(db2, "Select * from domain_rank where domain between 'google.com' and 'google.com.z'");
88+
if (rc != SQLITE_OK) {
89+
sqlite3_close(db1);
90+
sqlite3_close(db2);
91+
return;
92+
}
93+
rc = db_exec(db1, "Select * from surnames where name = 'SPRINGER'");
94+
if (rc != SQLITE_OK) {
95+
sqlite3_close(db1);
96+
sqlite3_close(db2);
97+
return;
98+
}
99+
rc = db_exec(db2, "Select * from domain_rank where domain = 'zoho.com'");
100+
if (rc != SQLITE_OK) {
101+
sqlite3_close(db1);
102+
sqlite3_close(db2);
103+
return;
104+
}
105+
106+
sqlite3_close(db1);
107+
sqlite3_close(db2);
108+
109+
}
110+
111+
void loop() {
112+
}

0 commit comments

Comments
 (0)