YUN: Sketch to read from SQLite3 database

Hello all,

For a few days I'm trying to find a way to read from a database via a sketch.
I found some examples WRITING to a database like

Tried it. Works fine.

But now, how to READ....?
I tried the following code that did not crash, but did not produce results as well.

void runSqlQuery_GetName()
{
  int    iNumber    = 0;
  String sName      = "";

  // Start a shell process to run a command in the OS.
  Process             p;
  
  // Which SQL engine are we going to use 
  String cmd          = "sqlite3 ";
  
  // commandline input
  String paramstring1 = "-line "  ;
  
  // set the path and name of your database here
  String paramstring2 ="/DB/test1.db ";
  
  // insert a row with a number and a name 
  String paramstring3 ="'SELECT * FROM Name ;'";
  
  // get the error code
  String paramstring4 =" ; echo $?";
  p.runShellCommand(cmd + paramstring1 + paramstring2 + paramstring3+ paramstring4);
  Serial.println("process run: '" + cmd + paramstring1 + paramstring2 + paramstring3+ paramstring4 + "'");
  // A process output can be read with the stream methods
  while (p.available()>0) {
    char c = p.read();
    if (c > 0)
    {
      Serial.print(c);
      break;
    }
  }
  // Ensure the last bit of data is sent.
  Serial.flush();
  Serial.println("Done");
}

Any advice would be greatly appreciated.

Friendly greetings,
Rens

opkg update
opkg install sqlite3-cli

root@Arduino:/usr/bin/sqlite3 /mnt/sda1/test.db

sqlite> create table tbl1(one varchar(10), two smallint);
sqlite> insert into tbl1 values('hello!',10);
sqlite> insert into tbl1 values('goodbye', 20);

use Control+Z to exit sqlite shell

root@Arduino:/usr/bin/sqlite3 -csv -header /mnt/sda1/test.db "select * from tbl1;"

#include <Process.h>

void setup() {
  Bridge.begin();	// Initialize the Bridge
  Serial.begin(9600);	// Initialize the Serial

  // Wait until a Serial Monitor is connected.
  while (!Serial);
}

void loop() {
  Process p;
  p.runShellCommand("/usr/bin/sqlite3 -csv -header /mnt/sda1/test.db \"select * from tbl1;\"");

  // do nothing until the process finishes, so you get the whole output:
  while (p.running());

  // Read command output. runShellCommand()
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);    
  }
  Serial.flush();
  Serial.println("Done");
  while(1) { };
}

Thanks. :slight_smile:

opkg update
opkg install sqlite3-cli

Had been done before, so that was okay

void runSqlQuery_GetName()
{
  int    iNumber    = 0;
  String sName      = "";

  // Start a shell process to run a command in the OS.
  Process p;
  String sCommand = "/usr/bin/sqlite3 -csv /DB/test1.db 'SELECT * FROM Name;' ";
  p.runShellCommand(sCommand);
  Serial.println   (sCommand);

  // do nothing until the process finishes, so you get the whole output:
  while (p.running());

  // Read command output. runShellCommand()
  while (p.available()>0) {
    char c = p.read();
    Serial.print(c);    
  }
  Serial.flush();
  Serial.println("Done");
}

Result:

1,Rens

I knew I was making things to difficult.
Thank you very much.

Hello RensD, can you please explain to me where you got the files who are on the www of the yunadventure subfolder ?
thank you very much for helping

Fbo06:
Hello RensD, can you please explain to me where you got the files who are on the www of the yunadventure subfolder ?
thank you very much for helping

Hi. Of cause I would like to help you, but I hac no idea what you are referring to.
please explain, so I can sit you.

friendly greeting,
Rens Duijsens