SELECT function

Hi,

Can anyone shed some light on how I can use the SELECT function?
If you could please give me an example of code.

Any help would be appreciated.

thanks

There is no SELECT function that I know of. Can you add a little context? Where have you seen this before?

I think you are thinking of C#, which does have a SELECT keyword. The C equivalent is the switch keyword. Try this:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("Enter 1, 2, or 3:");
}

void loop() {
  char buffer[10];
  int charsRead;
  int choice;

  if (Serial.available() > 0) {
    charsRead = Serial.readBytesUntil('\n', buffer, sizeof(buffer) - 1);
    buffer[charsRead] = '\0';   // This makes it a C string

    choice = buffer[0] - '0';   // ASCII for '1' is 49, '0' = 48  so 
                                // choice = 49 - 48 = 1, as an int, not a char

    switch (choice) {
      case 1:
        Serial.println("You entered a 1");
        break;
      case 2:
        Serial.println("You entered a 2");
        break;
      case 3:
        Serial.println("You entered a 3");
        break;
      default:
        Serial.println("You can't follow directions");
        break;
    }
  }

}

Hi,

i mean is :

#include "SPI.h"
#include "Ethernet.h"
#include "sha1.h"
#include "mysql.h"

byte mac_addr = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(10.0.1.2);

Connector my_conn;
char user = "root";
char password = "admin";
char QUERY1 = "select field1 from test1";

.....
.....

i want to output from select function above (QUERY1).

thank for your help.

Is this what you are looking for?

switch (var) {
    case 1:
      //do something when var equals 1
      break;
    case 2:
      //do something when var equals 2
      break;
    default: 
      // if nothing else matches, do the default
      // default is optional
    break;
  }

C is not SQL and does not support direct SQL keywords. Also, please read the two posts by Nick Gammon at the top of this Forum for posting guidelines, especially the use of code tags when posting source code. They will help us help you.

C is not SQL and does not support direct SQL keywords.

The mysql library does, though. It actually just passes the statements to the server for processing.

Help for the SELECT statement in SQL is readily available on the internet. What you can do with the statement is tightly coupled to the database you are working with.

raysha11:
Hi,

i mean is :

#include "SPI.h"
#include "Ethernet.h"
#include "sha1.h"
#include "mysql.h"

byte mac_addr = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(10.0.1.2);

Connector my_conn;
char user = "root";
char password = "admin";
char QUERY1 = "select field1 from test1";

.....
.....

i want to output from select function above (QUERY1).

thank for your help.

That's not a function. It's a string. It's just a collection of letters that some other function somewhere else must be intending to display somewhere.

Hi,

please give me an example of code. i want to output from query.

thanks

OK.

SELECT * FROM Customers WHERE Country='Mexico';

If you need to bone up on SQL, you may want to google something like "SQL tutorial".

This forum is for the arduino, but we try to help even if it is off topic a bit.

raysha11:
Hi,

please give me an example of code. i want to output from query.

thanks

From the code you posted? There is no output for that. It's just a string.

You could do this:

Serial.println(QUERY);

and this would be printed to the screen:

select field1 from test1

And that's about all you can do with it.

I would assume that somewhere in the rest of the code that you haven't shown us is some function that displays this message and then takes an input. Without seeing the code though, I can only guess.

Hi,

i try :
#include "SPI.h"
#include "Ethernet.h"
#include "sha1.h"
#include "mysql.h"

byte mac_addr = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(10.0.1.2);

Connector my_conn;
char user = "root";
char password = "admin";
char QUERY1 = "select field1 from test1";

Serial.println(QUERY);

result from script : "select field1 from test1" not data from table (query). so i want to "mysql_fetch_row".

thanks

Please use code tags.

Read this before posting a programming question

How to use this forum

please give me an example of code. i want to output from query.

Your questions are not making a lot of sense.

This is the PHP equivalent of what I think you are looking for:

<?php
$result = mysql_query("SELECT field1,field2 FROM table WHERE value = 'x'");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // field1
echo $row[1]; // field2
?>

It needs to be converted into Arduino speak.
Maybe somebody can translate…?

FWIW the above query assumes a single unique result i.e. one row.
For multiple returned rows, you would need to loop to extract the results.

result from script : "select field1 from test1" not data from table (query). so i want to "mysql_fetch_row".

You don't have a table named test1, it doesn't have a field1 column, or there is no data in the column, and yet you want to fetch that using mysql_fetch_row()? Not a snowball's chance in hell of that happening.