Re: Finding valid ENUM values
- From: Roedy Green <see_website@xxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 30 Sep 2007 23:19:59 GMT
On Sun, 30 Sep 2007 02:12:42 +0100, "Rupert Woodman" <NoEmail@com>
wrote, quoted or indirectly quoted someone who said :
I have a MySQL (5) database which has a table containing an ENUM column.
I'd like to (programatically) get a list of what the valid options are for
this ENUM. I've looked through the metadata but can't see anything. Can
someone point out what I'm missing?
You can write something like this:
/**
* stage codes, upper case
* U = untranslated
* ? = unsure of translation
* T = translated
* P = proofread (only proofreader/admin can set P or C status)
* C = complete (proofread in context of the actual program making
sure it fits in the box, looks good etc.)
*/
stage ENUM( 'U','?','T','P', 'C' ) NOT NULL,
The values you put and get will be ordinary strings.
There is an a abominably documented feature to use enums by index
rather than value. It follows advice in my "how to write
unmaintainable code" essay. Perhaps this could be worked up into a
method.
I found this snippet of PHP code. Perhaps you can decipher it and
produce a Java equivalent.
"I wrote this php function to return an array from enum values, using
the preg() solution posted by Willem-Jan van Dinter above, hope
someone finds it useful
Note: returns array(0 => 'None') if no results or no such table
function enum($object) {
list($table, $col) = explode(".", $object);
$row=@mysql_fetch_assoc(mysql_query("SHOW COLUMNS FROM ".$table." LIKE
'".$col."'"));
return ($row ?
explode("','",preg_replace("/(enum|set)\('(.+?)'\)/","\\2",$row['Type']))
: array(0=>'None'));
}
$optarray = enum("table.column");"
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
.
- Follow-Ups:
- Re: Finding valid ENUM values
- From: Arne Vajhøj
- Re: Finding valid ENUM values
- Next by Date: Re: (Mis)use of transactions
- Next by thread: Re: Finding valid ENUM values
- Index(es):