Eric Morand wrote:
Hi to all !

Is there a way to access to the datatype used in the CREATE TABLE statement dynamically ? [...]

http://sqlite.org/lang.html#pragma_show_datatypes


If you're using the new, non-callback based API you get the datatypes anyway.

[...] How do you guys deal with this problem ? In other word, how do you make sure that you are storing a value from the database in the RIGHT datatype variable - a string on a string variable, an int on an int variable...

My Python wrapper does this automatically:


http://sqlite.org/lang.html#pragma_show_datatypes

Based on the column type, I remember a so-called type code, which i later use to convert the string to the correct datatype. Two small excerpts from the code:

...


/* Try to determine column type. */ if (strstr(type_name, "INTERVAL")) { type_code = tc_INTERVAL; } else if (strstr(type_name, "INT")) { type_code = tc_INTEGER; } else if (strstr(type_name, "CHAR") || strstr(type_name, "TEXT")) { type_code = tc_STRING; } else if (strstr(type_name, "UNICODE")) { type_code = tc_UNICODESTRING; } else if (strstr(type_name, "BINARY") || strstr(type_name, "BLOB")) { type_code = tc_BINARY; }

...

else if (type_code == tc_INTEGER)
{
PyTuple_SetItem(p_row, i, Py_BuildValue("i",atol(p_fields[i])));
}
else if (type_code == tc_FLOAT)
{
PyTuple_SetItem(p_row, i, Py_BuildValue("f", atof(p_fields[i])));
}


...

I use the "type code" because it's stored in the meta data for the result set, not just for the heck of it ;-)

-- Gerhard

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to