ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
331 stars 132 forks source link

How to passing Integer values to sprintf #89

Closed 3rc0 closed 4 years ago

3rc0 commented 5 years ago

Example:

char UPDATE_DATA[] = " UPDATE dbName.dbName SET COL1 = %d, COL2 = %d WHERE COL3 = %d"; char query[128];

unsigned int num1 = 1; unsigned int num2 = 1111; unsigned int num3 = 5;

Is this one correct? sprintf(query,UPDATE_DATA, %d, %d, %d, num1, num2, num3);

or there is another way to pass integers to the query update?!

Bolukan commented 5 years ago

No not correct, check the syntax of sprintf, but sprintf (check also snprintf) is perfect to pass integers to a query. int sprintf(char *str, const char *format, ...);

so try: (const is not necessary, but just to be clear)

const char* UPDATE_DATA = "UPDATE dbName.dbName SET COL1 = %u, COL2 = %u WHERE COL3 = %u";
char query[128];
sprintf(query,UPDATE_DATA, num1, num2, num3);
ChuckBell commented 5 years ago

Correct. Just watch your length for the buffer (query).

On Wed, Mar 13, 2019 at 11:25 AM Bollie notifications@github.com wrote:

No not correct, check the syntax of sprintf https://linux.die.net/man/3/sprintf: int sprintf(char str, const char format, ...);

so try: (const is not necessary, but just to be clear)

const char* UPDATE_DATA = "UPDATE dbName.dbName SET COL1 = %u, COL2 = %u WHERE COL3 = %u"; char query[128]; sprintf(query,UPDATE_DATA, num1, num2, num3);

— 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/89#issuecomment-472470991, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0j4MseIWBcSgfmpGXIEi9NullzTg-Qks5vWRiFgaJpZM4bsbcj .

3rc0 commented 5 years ago

Thank you @Bolukan your method is working perfect ( I tested without const ). Thank you @ChuckBell for the quick response. The problem was I an issue with W5100 module, so I bought a new W5500 module and now everything works amazingly.

ChuckBell commented 5 years ago

Ok, can we close the issue?

On Wed, Mar 13, 2019 at 12:41 3rc0 notifications@github.com wrote:

Thank you @Bolukan https://github.com/Bolukan your method is working perfect ( I tested without const ). Thank you @ChuckBell https://github.com/ChuckBell for the quick response. The problem was I an issue with W5100 module, so I bought a new W5500 module and now everything works amazingly.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/89#issuecomment-472504891, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0j4MFSuwhwF0p8nqOSsq5txcOaXODzks5vWSocgaJpZM4bsbcj .

3rc0 commented 5 years ago

Yes.

3rc0 commented 5 years ago

@ChuckBell Is there possible to track usage of "query" length and bytes get it with Serial.print()? because I have multi statements and afraid with working on it will get issue?!

ChuckBell commented 5 years ago

You can use strlen(query), which will tell you how many characters (bytes for single-byte charsets) are in the string. So, you can check in advance or detect when you're overflowing your buffer.

JanissaryID commented 4 years ago

Please Help me for solve my problem char waktu[] = "09:00:00"; char UPDATE_SQL[] = "UPDATE employeedb.presence SET TimeIn = '%c' WHERE id = 1",waktu; its not correct

ChuckBell commented 4 years ago

You're trying to format a string. You need to use %s and sprintf(). See the example sketches. This technique is shown there.