Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-07

MySQL question...

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Rainer Hantsch

Hello, everybody!

Currently I am thinking about enhancing my MySQL unit, because it currently does only a very simple way of querying. I use Michael's "mysql" unit and made another "layer" on top of it, preventing me from doing too much particular things repeatedly.

My biggest problem is that this mysql unit returns the data in form of an array [0..n], which does not really make it easier to pick out a particular field. So I want to write a function which gets a field name passed and returns me the content. For that purpose I need to query the field names of a table, but I do not know, HOW.

Can somebody help, please?

mfg

Ing. Rainer Hantsch

\\|// Ing. Rainer HANTSCH - Hardware + Software (o o) Forget Windoze! -- We focus on L-I-N-U-X... --oOOo-(_)-oOOo-------------------------------------------------- Ing. Rainer HANTSCH | e-Mail: Khunngasse 21/20 | www : http://www.hantsch.co.at A-1030 Vienna | Tel. : ++43 - 1 - 7988538 0 ---------------------| Fax : ++43 - 1 - 7988538 18 ** AUSTRIA ** | Mobile: ++43 - 664 - 9194382 -----------------------------------------------------------------
Mirek Novak
to Rainer Hantsch
Rainer Hantsch wrote 20 lines
Hello, everybody! Currently I am thinking about enhancing my MySQL unit, because it currently does only a very simple way of querying. I use Michael's "mysql" unit and made another "layer" on top of it, preventing me from doing too much particular things repeatedly. My biggest problem is that this mysql unit returns the data in form of an array [0..n], which does not really make it easier to pick out a particular field. So I want to write a function which gets a field name passed and returns me the content. For that purpose I need to query the field names of a table, but I do not know, HOW. Can somebody help, please? mfg Ing. Rainer Hantsch

Take a look at mysql manual in section Mysql API - MySql C API u have list of functions with small examples - all u need. In your case U probably need function mysql_fetch_fields(...)

<FROM MANUAL>
8.4.3.16 mysql_fetch_fields()
MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *result) Description
Returns an array of all MYSQL_FIELD structures for a result set. Each structure provides the eld de nition for one column of the result set. Return Values
An array of MYSQL_FIELD structures for all columns of a result set. Errors
None.

Example

unsigned int num_fields;
unsigned int i;
MYSQL_FIELD *fields;
num_fields = mysql_num_fields(result);
fields = mysql_fetch_fields(result);
for(i = 0; i < num_fields; i++)
{
printf("Field %u is %s\n", i, fields[i].name);
}
</FROM MANUAL>
-- Mirek Novak Anima Publishers CMS development http://www.auto.cz http://www.formule1.cz ISEEKYOU:1<one>9<four>9<nine>44<eight> GEsEm:+420603807838
Michael Van Canneyt
to Rainer Hantsch: Hello, everybody! Currently I am thinking about enhancing my MySQL unit, because it…
Rainer Hantsch wrote 12 lines
Hello, everybody! Currently I am thinking about enhancing my MySQL unit, because it currently does only a very simple way of querying. I use Michael's "mysql" unit and made another "layer" on top of it, preventing me from doing too much particular things repeatedly. My biggest problem is that this mysql unit returns the data in form of an array [0..n], which does not really make it easier to pick out a particular field. So I want to write a function which gets a field name passed and returns me the content. For that purpose I need to query the field names of a table, but I do not know, HOW.

After the query has been run, run mysql_num_fields to get the number of fields. Then, for each field, run mysql_fetch_field to retrieve a pointer to a record describing the fields. This includes the field name. This way you can fill an array of field names and do the inverse search: for a field name, find the index in the array - this is easy.

Or you can use TDataset, which does all of this for you.

Michael.