| View previous topic :: View next topic |
| Author |
Message |
Saurabh Lahoti Guest
|
Posted: Wed Jun 08, 2005 2:07 pm Post subject: How to obtain the result of a 'SELECT' SQL query |
|
|
I was using a TQuery object to perform a 'SELECT' query, and wanted to know exactly how to access the result set afterwards ?
|
|
| Back to top |
|
 |
Don Locke Guest
|
Posted: Wed Jun 08, 2005 3:21 pm Post subject: Re: How to obtain the result of a 'SELECT' SQL query |
|
|
String sTmp = Query1->FieldByName("SelectedField")->AsString;
Works just like a Table.
HTH, Don
|
|
| Back to top |
|
 |
Saurabh Lahoti Guest
|
Posted: Wed Jun 08, 2005 3:33 pm Post subject: Re: How to obtain the result of a 'SELECT' SQL query |
|
|
The issue is that I didn't want to return information on just a field. I wanted to return a list of results from a precise SQL query statement. How would I obtain such a list that I could iterate through ?
Don Locke <dgnospamlocke (AT) comcast (DOT) net> wrote:
| Quote: | String sTmp = Query1->FieldByName("SelectedField")->AsString;
Works just like a Table.
HTH, Don
|
|
|
| Back to top |
|
 |
Daniel Mayo Guest
|
Posted: Wed Jun 08, 2005 3:46 pm Post subject: Re: How to obtain the result of a 'SELECT' SQL query |
|
|
Saurabh Lahoti wrote:
| Quote: | The issue is that I didn't want to return information on just a field. I wanted to return a list of results from a precise SQL query statement. How would I obtain such a list that I could iterate through ?
Don Locke <dgnospamlocke (AT) comcast (DOT) net> wrote:
String sTmp = Query1->FieldByName("SelectedField")->AsString;
Works just like a Table.
HTH, Don
The same way: |
int x;
Query1->First();
while (x<Query1->RecordCount)
{
String sTmp = Query1->FieldByName("SelectedField")->AsString;
//do something,..look at other fields,.. whatever
Query1->Next()
}
if you want something more basic.. like seeing it all in a stringgrid
automatically for ya,.. try associating a TDataSource object to your
TQuery then using one of the data controls objects like a TDBGrid and
associating that with your TDataSource.
voila. (works in design mode too so you can see what you are up to)
HTHs
Dan
|
|
| Back to top |
|
 |
Jayme Jeffman Filho Guest
|
Posted: Wed Jun 08, 2005 6:41 pm Post subject: Re: How to obtain the result of a 'SELECT' SQL query |
|
|
Hi Lahoti,
I am not sure I have understood your question, but, as far as I know, you
can not access the result set as a single string. You have to get the value
of each field, one by one using Query1->FieldByName("SelectedField")->As....
for each one. The As... property depends on the field type, although you can
convert all of them to strings using the AsString property.
HTH
Jayme.
"Saurabh Lahoti" <slahoti (AT) lexmark (DOT) com> escreveu na mensagem
news:42a70fcc$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
The issue is that I didn't want to return information on just a field. I
wanted to return a list of results from a precise SQL query statement. How |
would I obtain such a list that I could iterate through ?
| Quote: |
Don Locke <dgnospamlocke (AT) comcast (DOT) net> wrote:
String sTmp = Query1->FieldByName("SelectedField")->AsString;
Works just like a Table.
HTH, Don
|
|
|
| Back to top |
|
 |
Saurabh Lahoti Guest
|
Posted: Thu Jun 09, 2005 2:48 pm Post subject: Re: How to obtain the result of a 'SELECT' SQL query |
|
|
What if I had a query statement such as:
SELECT max(field_1)
FROM table_1
WHERE field_2 = 'name'
....I am just returning a single value that is has no field
name. What would I use in the place of the "SelectedField"
parameter ?
Daniel Mayo <dmayo (AT) reply (DOT) via.newsgroup.com> wrote:
| Quote: | Saurabh Lahoti wrote:
The issue is that I didn't want to return information on just a field. I wanted to return a list of results from a precise SQL query statement. How would I obtain such a list that I could iterate through ?
Don Locke <dgnospamlocke (AT) comcast (DOT) net> wrote:
String sTmp = Query1->FieldByName("SelectedField")->AsString;
Works just like a Table.
HTH, Don
The same way:
int x;
Query1->First();
while (x<Query1->RecordCount)
{
String sTmp = Query1->FieldByName("SelectedField")->AsString;
//do something,..look at other fields,.. whatever
Query1->Next()
}
if you want something more basic.. like seeing it all in a stringgrid
automatically for ya,.. try associating a TDataSource object to your
TQuery then using one of the data controls objects like a TDBGrid and
associating that with your TDataSource.
voila. (works in design mode too so you can see what you are up to)
HTHs
Dan
|
|
|
| Back to top |
|
 |
Daniel Mayo Guest
|
Posted: Thu Jun 09, 2005 3:27 pm Post subject: Re: How to obtain the result of a 'SELECT' SQL query |
|
|
Saurabh Lahoti wrote:
| Quote: | What if I had a query statement such as:
SELECT max(field_1)
FROM table_1
WHERE field_2 = 'name'
...I am just returning a single value that is has no field
name. What would I use in the place of the "SelectedField"
parameter ?
|
You have to specify a field name parameter (or use an index)..
I'd suggest the following:
TQuery *dQuery = TQUERYOBJECTNAME;
int MaxResult;
dQuery->SQL->Text =" SELECT max(field_1) as 'MaxField1'"
" FROM table_1"
" WHERE field_2 = 'name'";
dQuery->Active = true;
MaxResult = dQuery->Fields->FieldByName("MaxField1").AsInteger
HTHs
|
|
| Back to top |
|
 |
Jayme Jeffman Filho Guest
|
Posted: Fri Jun 10, 2005 3:34 pm Post subject: Re: How to obtain the result of a 'SELECT' SQL query |
|
|
Hi Lahoti,
Have a look in the C++Builder Help file, look for "TDataSet::Fields" and
check the example. It has a way of get access to every TQuery field, as long
as TTquery is TDataSet descendant.
HTH
Jayme.
"Saurabh Lahoti" <slahoti (AT) lexmark (DOT) com> escreveu na mensagem
news:42a856b4$1 (AT) newsgroups (DOT) borland.com...
| Quote: |
What if I had a query statement such as:
SELECT max(field_1)
FROM table_1
WHERE field_2 = 'name'
...I am just returning a single value that is has no field
name. What would I use in the place of the "SelectedField"
parameter ?
Daniel Mayo <dmayo (AT) reply (DOT) via.newsgroup.com> wrote:
Saurabh Lahoti wrote:
The issue is that I didn't want to return information on just a field.
I wanted to return a list of results from a precise SQL query statement. How |
would I obtain such a list that I could iterate through ?
| Quote: |
Don Locke <dgnospamlocke (AT) comcast (DOT) net> wrote:
String sTmp = Query1->FieldByName("SelectedField")->AsString;
Works just like a Table.
HTH, Don
The same way:
int x;
Query1->First();
while (x<Query1->RecordCount)
{
String sTmp = Query1->FieldByName("SelectedField")->AsString;
//do something,..look at other fields,.. whatever
Query1->Next()
}
if you want something more basic.. like seeing it all in a stringgrid
automatically for ya,.. try associating a TDataSource object to your
TQuery then using one of the data controls objects like a TDBGrid and
associating that with your TDataSource.
voila. (works in design mode too so you can see what you are up to)
HTHs
Dan
|
|
|
| Back to top |
|
 |
|