Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top