Errors when working on SQLITE with esp32

monish
Posts: 2
Joined: Sat Dec 22, 2018 7:21 am

Errors when working on SQLITE with esp32

Postby monish » Tue Jan 08, 2019 11:33 am

Hi,

I am trying to use SQLITE with esp32. I have used the following code.
  1. /*
  2.     This example creates two databases on SPIFFS,
  3.     inserts and retrieves data from them.
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/unistd.h>
  8. #include <sys/stat.h>
  9. #include "esp_err.h"
  10. #include "esp_log.h"
  11. #include "esp_spiffs.h"
  12. #include "esp_timer.h"
  13. #include "sqlite3.h"
  14. #include "esp32.c"
  15. #include "sqlite3.c"
  16.  
  17. static const char *TAG = "sqlite3_spiffs";
  18.  
  19. const char* data = "Callback function called";
  20. static int callback(void *data, int argc, char **argv, char **azColName) {
  21.    int i;
  22.    printf("%s: ", (const char*)data);
  23.    for (i = 0; i<argc; i++){
  24.        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  25.    }
  26.    printf("\n");
  27.    return 0;
  28. }
  29.  
  30. int db_open(const char *filename, sqlite3 **db) {
  31.    int rc = sqlite3_open(filename, db);
  32.    if (rc) {
  33.        printf("Can't open database: %s\n", sqlite3_errmsg(*db));
  34.        return rc;
  35.    } else {
  36.        printf("Opened database successfully\n");
  37.    }
  38.    return rc;
  39. }
  40.  
  41. char *zErrMsg = 0;
  42. int db_exec(sqlite3 *db, const char *sql) {
  43.    printf("%s\n", sql);
  44.    int64_t start = esp_timer_get_time();
  45.    int rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);
  46.    if (rc != SQLITE_OK) {
  47.        printf("SQL error: %s\n", zErrMsg);
  48.        sqlite3_free(zErrMsg);
  49.    } else {
  50.        printf("Operation done successfully\n");
  51.    }
  52.    printf("Time taken: %lld\n", esp_timer_get_time()-start);
  53.    return rc;
  54. }
  55.  
  56. void app_main()
  57. {
  58.     sqlite3 *db1;
  59.     sqlite3 *db2;
  60.     int rc;
  61.  
  62.     ESP_LOGI(TAG, "Initializing SPIFFS");
  63.    
  64.     esp_vfs_spiffs_conf_t conf = {
  65.       .base_path = "/spiffs",
  66.       .partition_label = "storage",
  67.       .max_files = 5,
  68.       .format_if_mount_failed = true
  69.     };
  70.    
  71.     // Use settings defined above to initialize and mount SPIFFS filesystem.
  72.     // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  73.     esp_err_t ret = esp_vfs_spiffs_register(&conf);
  74.  
  75.     if (ret != ESP_OK) {
  76.         if (ret == ESP_FAIL) {
  77.             ESP_LOGE(TAG, "Failed to mount or format filesystem");
  78.         } else if (ret == ESP_ERR_NOT_FOUND) {
  79.             ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  80.         } else {
  81.             ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  82.         }
  83.         return;
  84.     }
  85.    
  86.     size_t total = 0, used = 0;
  87.     ret = esp_spiffs_info(NULL, &total, &used);
  88.     if (ret != ESP_OK) {
  89.         ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
  90.     } else {
  91.         ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
  92.     }
  93.  
  94.     // remove existing file
  95.     unlink("/spiffs/test1.db");
  96.     unlink("/spiffs/test2.db");
  97.  
  98.     sqlite3_initialize();
  99.  
  100.     if (db_open("/spiffs/test1.db", &db1))
  101.         return;
  102.     if (db_open("/spiffs/test2.db", &db2))
  103.         return;
  104.  
  105.     rc = db_exec(db1, "CREATE TABLE test1 (id INTEGER, content);");
  106.     if (rc != SQLITE_OK) {
  107.       //  sqlite3_close(db1);
  108.        // sqlite3_close(db2);
  109.         return;
  110.     }
  111.     rc = db_exec(db2, "CREATE TABLE test2 (id INTEGER, content);");
  112.     if (rc != SQLITE_OK) {
  113.         //sqlite3_close(db1);
  114.         //sqlite3_close(db2);
  115.         return;
  116.     }
  117.  
  118.     rc = db_exec(db1, "INSERT INTO test1 VALUES (1, 'Hello, World from test1');");
  119.     if (rc != SQLITE_OK) {
  120.         //sqlite3_close(db1);
  121.         //sqlite3_close(db2);
  122.         return;
  123.     }
  124.     rc = db_exec(db2, "INSERT INTO test2 VALUES (1, 'Hello, World from test2');");
  125.     if (rc != SQLITE_OK) {
  126.         //sqlite3_close(db1);
  127.         //sqlite3_close(db2);
  128.         return;
  129.     }
  130.  
  131.     rc = db_exec(db1, "SELECT * FROM test1");
  132.     if (rc != SQLITE_OK) {
  133.         //sqlite3_close(db1);
  134.         //sqlite3_close(db2);
  135.         return;
  136.     }
  137.     rc = db_exec(db2, "SELECT * FROM test2");
  138.     if (rc != SQLITE_OK) {
  139.         //sqlite3_close(db1);
  140.         //sqlite3_close(db2);
  141.         return;
  142.     }
  143.  
  144.     //sqlite3_close(db1);
  145.     //sqlite3_close(db2);
  146.  
  147.     // All done, unmount partition and disable SPIFFS
  148.     esp_vfs_spiffs_unregister(NULL);
  149.     ESP_LOGI(TAG, "SPIFFS unmounted");
  150.  
  151.     while(1);
  152. }

and i am getting these errors. Please let me know what modifications are required for this code to work.
  1. LD build/sqlite3_sdmmc.elf
  2. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o):(.literal.openDb+0x8): undefined reference to `sqlite3_open'
  3. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o):(.literal.openDb+0xc): undefined reference to `sqlite3_errmsg'
  4. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o):(.literal.db_exec+0x18): undefined reference to `sqlite3_exec'
  5. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o):(.literal.db_exec+0x1c): undefined reference to `sqlite3_free'
  6. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o):(.literal.app_main+0x40): undefined reference to `sqlite3_initialize'
  7. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o):(.literal.app_main+0x44): undefined reference to `sqlite3_close'
  8. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o): In function `openDb':
  9. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:36: undefined reference to `sqlite3_open'
  10. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:38: undefined reference to `sqlite3_errmsg'
  11. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o): In function `db_exec':
  12. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:50: undefined reference to `sqlite3_exec'
  13. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:52: undefined reference to `sqlite3_free'
  14. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/main/libmain.a(sd_mmc.o): In function `app_main':
  15. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:109: undefined reference to `sqlite3_initialize'
  16. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:126: undefined reference to `sqlite3_close'
  17. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:127: undefined reference to `sqlite3_close'
  18. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:132: undefined reference to `sqlite3_close'
  19. /home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/main/sd_mmc.c:138: undefined reference to `sqlite3_close'
  20. collect2: error: ld returned 1 exit status
  21. /home/dz/esp/esp-idf/make/project.mk:453: recipe for target '/home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/sqlite3_sdmmc.elf' failed
  22. make: *** [/home/dz/esp/esp-idf/examples/esp32-idf-sqlite3-master/esp32-idf-sqlite3-examples-master/sd_mmc/build/sqlite3_sdmmc.elf] Error 1

Who is online

Users browsing this forum: No registered users and 104 guests