Class to manager MYSQL for Harbour/xHarbour
Custom Search

martes, 6 de julio de 2010

Resultado de la consulta / Retrieve query

//Español
ya hemos visto lo facil que es crear una consulta (query)

Para generar la consulta podemos usar directamente la clase TDolphinQry
o llamar al Metodo Query desde el objeto servidor,
en ambos casos nos devuelve un objeto TDolphinQry

oQry = TDolphinQry():New( cQuery, oServer )
oQry = oServer:Query( cQuery )

ahora demostraremos como obtener informacion de la consulta

abarcaremos la parte basica de un query WHERE, GROUP, HAVING, ORDER, LIMIT
y podran ser consultados y/o cambiados dinamicamente.
La estructura de los campos e una consulta sera convertida a una estructura
de campos reconocida por [x]Harbour, dicha estructura sera llenada automaticamente
y guardada en la data aStruture

veamos un ejemplo...

//English
we seen how easy it is to create a query
To build the query can be directly used TDolphinQry class
or call Query Method from the object server
in both cases we returns an TDolphinQry object


oQry = TDolphinQry ():New (cQuery, oServer)
oQry = oServer: Query (cQuery)


now show how to obtain information from the query


will cover the basic parts of a query WHERE, GROUP, HAVING, ORDER, LIMIT
and may be consulted and / or changed dynamically.
The query field structure will be converted to a field structure
recognized by [x] Harbour, this structure will be filled automatically
and stored in the data aStruture


para mas detalles revisar MySql field structure en tdolphin.ch
for more detail review MySql field structure in tdolphin.ch

Ejemplo/sample 

Download here

// define query tables
aTables  = { "grade_event", "score", "student" }

//define query columns (from)
aColumns = { "student.student_id", "student.name", "grade_event.date", "score.score", "grade_event.category"}

//define where
cWhere   = "grade_event.date = " + ClipValue2SQL( CToD( '09-23-2008' ) ) + " and "
cWhere  += "grade_event.event_id = score.event_id and score.student_id = student.student_id"

//let Dolphin build query by us, remmenber can build the query your self
cQuery = BuildQuery( aColumns, aTables, cWhere )

//build query object
oQry = TDolphinQry():New( cQuery, oServer )

FOR EACH aRow IN oQry:aStructure
  FOR EACH uItem IN aRow
     ?? If( ValType( uItem ) == "N", PadR( AllTrim( Str( uItem ) ), 10 ), PadR( uItem, 11 ) )
  NEXT
  ?
NEXT


   //Retrieve cWhere
   ? "WHERE" 
   ? oQry:cWhere 


   //Retrieve Info, we can use fieldget( cnField )
   ? 
   ? oQry:student_id, oQry:name, oQry:date, oQry:score, oQry:category
   ? oQry:FieldGet( 1 ), oQry:FieldGet( 2 ), oQry:FieldGet( "date" ), oQry:FieldGet( 4 ), oQry:FieldGet( "category" )
   



   // moving recond pointer
   ? oQry:Goto( 5 ), "GoTo 5"
   ? oQry:GoTop(), "GoTop()"
   ? oQry:GoBottom(), "GoBottom()"
   ? oQry:Skip( - 3 ), "Skip -3"
   ? 





//browsing
   oQry:GoTop()
   ? "student_id  name        date    score    category"
   ? "==================================================="
   do while !oQry:Eof()
      ? oQry:student_id, oQry:name, oQry:date, oQry:score, oQry:category
      oQry:Skip()
   end
   ? "==================================================="








No hay comentarios:

Publicar un comentario