ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
332 stars 133 forks source link

How get string fro database #46

Closed damian1538 closed 2 years ago

damian1538 commented 6 years ago

Hello. Please help. After 3 days of fighting, it's time to ask smarter. How to download a string from the database to display it in a serial. I am asking for an example.

include

include

include

int ledpin=2; byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(194,181,122,197);
char user[] = "r"; // MySQL user login username char password[] = "s"; // MySQL user login password

// Sample query char query[] = "SELECT num FROM test_arduino.hello_arduino WHERE num='1'";

EthernetClient client; MySQL_Connection conn((Client *)&client); // Create an instance of the cursor passing in the connection MySQL_Cursor cur = MySQL_Cursor(&conn);

void setup() { pinMode(ledpin, OUTPUT); Serial.begin(115200); while (!Serial); // wait for serial port to connect Ethernet.begin(mac_addr); Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, user, password)) { delay(200); } else Serial.println("Connection failed."); }

void loop() { row_values *row = NULL; long head_count = 0;

delay(1000);

Serial.println("---"); // Execute the query cur.execute(query); // Fetch the columns (required) but we don't use them. cur.get_columns(); // Read the row (we are only expecting the one) do { row = cur.get_next_row();

if (row != NULL) {
  head_count = atol(row->values[0]);
}

} while (row != NULL); // Now we close the cursor to free any memory

cur.close();

// Show the result but this time do some math on it Serial.print(" Wartosc z bazy: "); cur.read_string(); Serial.println(head_count); if(head_count == 0) { digitalWrite(ledpin, LOW); }else{ digitalWrite(ledpin, HIGH); }

}

ChuckBell commented 6 years ago

Hi,

I'm happy to help, but I need more information.

It appears you have looked at the example sketch, "basic_select". What exactly is the problem you're experiencing? Please include any output and input (the data in the table).

What are you trying to do with cur.read_string();? That isn't a valid method in the Cursor class.

If you are just retrieving a string, you can assign a pointer to it and use that in the Serial.print() method. Once I see the sample data, I may be able to get you some code that works (better).

Also, which Arduino board and shield are you using?

Dr. Bell

On 6/14/18 2:14 PM, damian1538 wrote:

Hello. Please help. After 3 days of fighting, it's time to ask smarter. How to download a string from the database to display it in a serial. I am asking for an example.

include

include

include

int ledpin=2; byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(194,181,122,197); char user[] = "r"; // MySQL user login username char password[] = "s"; // MySQL user login password

// Sample query char query[] = "SELECT num FROM |test_arduino|.|hello_arduino| WHERE num='1'";

EthernetClient client; MySQL_Connection conn((Client *)&client); // Create an instance of the cursor passing in the connection MySQL_Cursor cur = MySQL_Cursor(&conn);

void setup() { pinMode(ledpin, OUTPUT); Serial.begin(115200); while (!Serial); // wait for serial port to connect Ethernet.begin(mac_addr); Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, user, password)) { delay(200); } else Serial.println("Connection failed."); }

void loop() { row_values *row = NULL; long head_count = 0;

delay(1000);

Serial.println("---"); // Execute the query cur.execute(query); // Fetch the columns (required) but we don't use them. cur.get_columns(); // Read the row (we are only expecting the one) do { row = cur.get_next_row();

|if (row != NULL) { head_count = atol(row->values[0]); } |

} while (row != NULL); // Now we close the cursor to free any memory

cur.close();

// Show the result but this time do some math on it Serial.print(" Wartosc z bazy: "); cur.read_string(); Serial.println(head_count); if(head_count == 0) { digitalWrite(ledpin, LOW); }else{ digitalWrite(ledpin, HIGH); }

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/46, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0j4IUUKEEvjuv26asSEuTJsG3Gcv87ks5t8qgcgaJpZM4UobJd.

damian1538 commented 6 years ago

I'm a beginner. I need to read the string from the table. I want to point a particular cell from the base. I need to have it in the string to display it on the display screen. something like an instant messenger. I am asking for a piece of code to download thongs and subtitles to use them later

damian1538 commented 6 years ago

Here is an example of another library, except that it does not work. :(

include

char host, user, pass, db;

void setup() { Serial.begin(9600); host = "localhost"; user = "root"; pass = ""; db = "arduino"; if(mysql_connect(host,user,pass,db){

    Serial.print("Connected to ");
    Serial.println(host);
    int result = mysql_result_query("SELECT FROM `sensors` WHERE `id` = 1", "value");
    if(result){
        Serial.print("Value of the sensor is:");
        Serial.println(result);
    }else{
        Serial.println("Query exexcution failed or no record(s) found.");
    }
}else{
    Serial.println("Connection failed.");
}

}

void loop(){}

ChuckBell commented 6 years ago

Please PM me at d r c h a r l e s b e l l at g m a i l dot c o m with your existing sketch and I’ll try to put an example together for you.

Dr Bell

On Fri, Jun 15, 2018 at 18:17 damian1538 notifications@github.com wrote:

Here is an example of another library, except that it does not work. :(

include

char host, user, pass, db;

void setup() { Serial.begin(9600); host = "localhost"; user = "root"; pass = ""; db = "arduino"; if(mysql_connect(host,user,pass,db){

Serial.print("Connected to "); Serial.println(host); int result = mysql_result_query("SELECT FROM sensors WHERE id = 1", "value"); if(result){ Serial.print("Value of the sensor is:"); Serial.println(result); }else{ Serial.println("Query exexcution failed or no record(s) found."); } }else{ Serial.println("Connection failed."); }

}

void loop(){}

You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/46#issuecomment-397756108, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0j4OYkkeQTxMjvGk3Kvyqv5JYunQhhks5t9DJ0gaJpZM4UobJd .

ChuckBell commented 6 years ago

Well, here is another example. This reads strings from the database and prints them.

/* MySQL Connector/Arduino Example : retrieve and print a string

Created by: Dr. Charles A. Bell */

include

include

include

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192,168,42,XXX); // IP of the MySQL server here char user[] = "root"; // MySQL user login username char password[] = "XXXXXXX"; // MySQL user login password

// Sample query char query[] = "SELECT name FROM world.city WHERE CountryCode = 'USA' LIMIT 12";

EthernetClient client; MySQL_Connection conn((Client *)&client);

void show_strings_from_table() { char city_name[64]; row_values *row = NULL;

delay(1000); Serial.println("Demonstrating using a cursor dynamically allocated to get strings from table."); // Initiate the query class instance MySQL_Cursor cur_mem = new MySQL_Cursor(&conn); // Execute the query cur_mem->execute(query); // Fetch the columns (required) but we don't use them. column_names columns = cur_mem->get_columns();

Serial.println("Twelve city names from USA."); // Read the row (we are only expecting the one) do { row = cur_mem->get_next_row(); if (row != NULL) { // Copying the string from the first column returned to a local variable strcpy(city_name, row->values[0]); Serial.print("> "); Serial.println(city_name); } } while (row != NULL); // Deleting the cursor also frees up memory used delete cur_mem; }

void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect Ethernet.begin(mac_addr); Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, user, password)) { delay(1000); show_strings_from_table(); } else Serial.println("Connection failed."); conn.close(); }

void loop() { }

And here is the sample output as seen in the serial monitor:

Connecting... Connected to server version 8.0.3-rc-log Demonstrating using a cursor dynamically allocated to get strings from table. Twelve city names from USA.

New York Los Angeles Chicago Houston Philadelphia Phoenix San Diego Dallas San Antonio Detroit San Jose Indianapolis Disconnected.

If you want to run this sketch, you will need the world sample database from dev.mysql.com. And you will need to change the IP address to match your server as well as the user and password.

I'm not sure how much more help you need, but this answers your original query. Please consider closing this ticket and PM'ing me directly for more help.

mydhitz commented 6 years ago

Try this code ................................................................................... char query[128]; char SELECT_SQL[] = "SELECT NOW()"; ...................................................................................

` Serial.println("-> Connected");

MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
sprintf(query, SELECT_SQL);
cur_mem->execute(query);
column_names *cols = cur_mem->get_columns();    
Serial.println();
row_values *row = NULL;
row = cur_mem->get_next_row();
Serial.println(row->values[0]);
String nilai = row->values[0];          // -------> This nilai Variable String from db
int hasil = atoi(nilai.c_str());`       // --------> This hasil Variable Int from db
ChuckBell commented 6 years ago

Do you still need help or can I close this issue?

Sent from my iPad

On Jul 18, 2018, at 12:16 AM, dito notifications@github.com wrote:

Try this code ` Serial.println("-> Connected");

MySQL_Cursor cur_mem = new MySQL_Cursor(&conn); sprintf(query, STATUS_SQL); cur_mem->execute(query); column_names cols = cur_mem->get_columns();
Serial.println(); row_values *row = NULL; row = cur_mem->get_next_row(); Serial.println(row->values[0]); String nilai = row->values[0]; // -------> This nilai Variable String from db int hasil = atoi(nilai.c_str());` // --------> This hasil Variable Int from db — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.