Skip to content

simple query example, writing bytes #6

@petaflot

Description

@petaflot

This is sort of a noob question and not really an issue...

PgConsole is cool, but not really helpful in production : I can't imagine a case where I would use a microcontroller on a serial terminal to interract with my postgresql server ; however, pulling data from the server or logging stuff to it likely to be useful and used quite a lot.

The issue I'm facing (C++ is not one of my strong suits, more of a python dude) is a simple insert statement with bytes. Below is a modified version of doPG() that supposedly logs a message to postgresql ; in my case all query arguments are bytes, but I suppose examples with other data types would be appreciated by people living "in outer space".

the code below sends a string and not bytes. Again, C++ is really not something I feel comfortable with.

I suggest adding an example to the doc, next to PgConsole, similar to the code below (another example with "SELECT rows FROM table" with row content assigned to variables would be cool) :

int pg_status = 0;                   
void pgLogger( char message[] ) {    
#ifndef USE_ARDUINO_ETHERNET         
        checkConnection();           
        if (WiFiStatus == WL_CONNECTED) {
#endif                               
                char *msg;           
                int rc;
                if (!pg_status) {
                        conn.setDbLogin(PGIP,
                                user,
                                password,
                                dbname,
                                "utf8");
                        pg_status = 1;
                        return;
                }
                if (pg_status == 2) {
                        // TODO convert message to bytes
                        if (conn.execute(String("INSERT into mytable ( col0 ) VALUES (")+message+String("');"))) goto error;
                        Serial.println("Working...");
                        pg_status = 3;
                }
                if (pg_status == 3) {
                        rc=conn.getData();
                        int i;
                        if (rc < 0) goto error;
                        if (!rc) return;
                        if (rc & PG_RSTAT_HAVE_COLUMNS) {
                                for (i=0; i < conn.nfields(); i++) {
                                        if (i) Serial.print(" | ");
                                        Serial.print(conn.getColumn(i));
                                }
                                Serial.println("\n==========");
                        }
                        else if (rc & PG_RSTAT_HAVE_ROW) {
                                for (i=0; i < conn.nfields(); i++) {
                                        if (i) Serial.print(" | ");
                                        msg = conn.getValue(i);
                                        if (!msg) msg=(char *)"NULL";
                                        Serial.print(msg);
                                }
                                Serial.println();
                        }
                        else if (rc & PG_RSTAT_HAVE_SUMMARY) {
                                Serial.print("Rows affected: ");
                                Serial.println(conn.ntuples());
                        }
                        else if (rc & PG_RSTAT_HAVE_MESSAGE) {
                                msg = conn.getMessage();
                                if (msg) Serial.println(msg);
                        }
                }
                return;
#ifndef USE_ARDUINO_ETHERNET
        }
#endif

error:  
        msg = conn.getMessage();
        if (msg) Serial.println(msg);
        else Serial.println("UNKNOWN ERROR");
        if (conn.status() == CONNECTION_BAD) {
                Serial.println("Connection is bad");
                pg_status = -1;
        }
}                                    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions