2

I'm using MySQL C API with C++ to connect to a database. I wold like so select some numbers from the database.

Is there a way to fetch number types like INTEGER or DOUBLE into native C types like int or double directly without having to parse them from a string?

EDIT: How about ENUMs? I would hate do do a strcmp every time instead of using switch.

2 Answers 2

4

Thank you, but actually I think I just found a way while looking for something different...

Prepared statements can do the job. When selecting data the results are stored in a MYSQL_BIND, just like the prepared statement arguments. And in MYSQL_BIND the values are stored as native C types. I think these links are helpful:

http://lgallardo.com/en/2011/06/23/sentencias-preparadas-de-mysql-en-c-ejemplo-completo/ http://dev.mysql.com/doc/refman/5.5/en/mysql-stmt-fetch.html#id1363684

Sign up to request clarification or add additional context in comments.

Comments

0

I don't believe so - As far as I'm aware everything gets returned as a char * which is then up to you to interpret.

If you are fetching values that relate to an enum then use

int res = atoi(row[0])

then

switch(res)
{
case ENUM_VAL_1:
break;
}

For doubles, use something like strtod() to convert from char *. (This also checks for non-numbers too)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.