FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour ejemplo con postgresql
Posts: 132
Joined: Sun Oct 23, 2005 04:09 PM
ejemplo con postgresql
Posted: Tue Apr 06, 2010 11:36 PM

Hola.
He estado analizando hacia que bd migrar mi aplicación, he decidido jugarmela con postgres, para lo que necesito si tienen un ejemplo practico para accesar, grabar, modificar, etc. , o es que tambien tengo que cambiar el lenguaje?
saludos

Posts: 337
Joined: Fri Oct 07, 2005 02:44 PM
Re: ejemplo con postgresql
Posted: Thu Apr 08, 2010 04:13 PM

Hola Miguel,

Antes de responderte, unas consultas :

  • ¿ Como te vas a conectar a postgres ? , con driver nativo ( existe en harbour una serie de clases que estan operando ), con Ado, usando el driver odbc o algun dbprovider.

  • ¿ Que usas para programar ? harbour o xharbour

Yo uso harbour y para el driver nativo puedes ver los ejemplos en hbpgsql , si vas a usar ado, en este foro puedes usar cualquier ejemplo sobre eso y solo tienes que cambiar la cadena de coneccion.

Espero haberte ayudado un poco,

Lautaro Moreira

Hola,

Soy un Contador que por necesidad aprendio a programar y se quedo programando.
Posts: 132
Joined: Sun Oct 23, 2005 04:09 PM
Re: ejemplo con postgresql
Posted: Mon Apr 12, 2010 02:15 PM

Hola Lautaro

Tengo Fivewin-xHarbour con dbf actualmente
En cuanto a la conexion , no tengo experiencia en base de datos, mi intención es poner mi aplicacion en Cliente/Servidor, he visto que con postgres usando SQLRDD, la pregunta va mas enfocada a que alguien que tenga corriendo una aplicacion con fivewin y postgres y algun ejemplo del manejo de tablas.
gracias

Posts: 410
Joined: Sun Jan 31, 2010 03:30 PM
Re: ejemplo con postgresql
Posted: Sat Apr 17, 2010 04:21 PM

include "sqlrdd.ch"

include "mysql.ch"

define RECORDS_IN_TEST 1000

define SQL_DBMS_NAME 17

define SQL_DBMS_VER 18

REQUEST SQLRDD, SR_MYSQL

/------------------------------------------------------------------------/

Function Main( cDSN, lLog )

local apCode, oSqlCon , oRddCon , oSqlRdd, oSqlWeb
local nCnn, i

//***INICIO CONEXION CON DB CON POSTGRES SQLRDD

? "Connecting to database SQLRDD...postgres"

cConnString1 := "PGS=192.168.2.11;UID=user_comparta;PWD=123;DTB=xhb"

oSqlRdd := ConectaSqlRdd(cConnString1)

SR_GetConnection( oSqlRdd )

? "Connected to SQLRDD :", SR_GetConnectionInfo(, SQL_DBMS_NAME ), SR_GetConnectionInfo(, SQL_DBMS_VER )

USE "TEST_TABLE" EXCLUSIVE VIA "SQLRDD" NEW ALIAS TEST

BROWSE()

// CODIFIQUE IGUAL QUE CON DBF...

//***FIN CONEXION CON DB CON POSTGRES

*/

//***INICIO CONEXION CON DB CON MYSQL NO SQLRDD

? "Connecting to database NO SQLRDD...mysql"

cConnString2 := "MySQL=200.00.888.121;UID=user_zerus;PWD=123;DTB=db_zerus"

oSqlWeb := ConectaSqlRdd(cConnString2)

oSql := SR_GetConnection( oSqlWeb )

? "Connected to NO SQLRDD :", SR_GetConnectionInfo(, SQL_DBMS_NAME ),SR_GetConnectionInfo(, SQL_DBMS_VER )

apCode := SR_SQLParse( "SELECT * FROM personas WHERE ccodigocli = ?")

cSql := SR_SQLCodeGen( apCode,{"1"}, oSql:nSystemID )

osql:exec(cSql,,.t.,,'arquivo.dbf','temp') // ...ok

//aRet:={}
//osql:exec(cSql,,.t.,@aret) // ..OK
// alert( len(aRet) )

// use (csql) shared new alias temp via "SQLRDD" // ...ok

sele temp
browse(5,5,20,75)

apCode := SR_SQLParse( "UPDATE personas SET cnombrecli = ? WHERE ccodigocli = ?")
oSql := SR_GetConnection( oSqlWeb )
cSql := SR_SQLCodeGen( apCode,{"la juana VALENTINA 4","1"}, oSql:nSystemID )
osql:exec(cSql)

temp->( DBCOMMIT() )

TEMP->( DBGOTOP() )

DO WHILE TEMP->( !EOF() )

  ? TEMP->CNOMBRECLI

  TEMP->( DBSKIP() )

ENDDO

INKEY(0)

CLOSE ALL

// CODIFIQUE IGUAL QUE CON DBF...

//***FIN CONEXION CON DB CON MYSQL NO SQLRDD

Return NIL


FUNCTION ConectaSqlRdd(cConnString)

LOCAL nDetected

nDetected := DetectDBFromDSN( cConnString )

If nDetected > SYSTEMID_UNKNOW

  oCon := SR_AddConnection( nDetected, cConnString )

ELSE

  alert("Error de conexi¢n")

  return -1

EndIf

If oCon < 0

  alert(&quot;Error al conectarse a la base de datos de MySql : &quot; )
  CLS
  Quit

EndIf

return oCon


#include "connect.prg"

/ IMPORTANT: THIS FILE IS NOT TO BE USED DIRECTLY. YOU SHOULD INCLUDE IT IN PROPER APPLICATION PRG FILE
AS SHOWN IN DEMO01.PRG, PARSER2.PRG, MEMO.PRG, ETC., AND CALL CONNECT() FUNCTION.
/

/
* SQLRDD connection
* Sample applications connect routine
* Copyright (c) 2005 - Marcelo Lombardo <lombardo@uol.com.br>
* All Rights Reserved
/

include "pgs.ch" // Needed if you plan to use native connection to Postgres

include "mysql.ch" // Needed if you plan to use native connection to MySQL

include "oracle.ch" // Needed if you plan to use native connection to Oracle

include "firebird.ch" // Needed if you plan to use native connection to Firebird

REQUEST SQLRDD // SQLRDD should be linked in
REQUEST SR_ODBC // Needed if you plan to connect with ODBC
REQUEST SR_PGS // Needed if you plan to use native connection to Postgres
REQUEST SR_MYSQL // Needed if you plan to use native connection to MySQL
REQUEST SR_ORACLE // Needed if you plan to use native connection to Oracle
REQUEST SR_FIREBIRD // Needed if you plan to use native connection to Firebird

REQUEST DBFNTX
REQUEST DBFCDX
REQUEST DBFFPT
REQUEST DBFDBT

/------------------------------------------------------------------------/

Function Connect( cDatabase )

local nCnn, nDrv, cDriver, nOpt, nDetected, hIniFile, aKeys, nKey, cConnString
local oldScreen, hDsn

SetMode( 35, 80 )

hIniFile := HB_ReadIni( "sqlrdd.ini", .F.,,.F. ) // Read ini file in a hash table

If hIniFile == NIL
? "Could not read from sqlrdd.ini"
Quit
EndIf

If cDatabase == NIL
aKeys := HGetKeys( hIniFile )
If len(aKeys) == 0
? "No connections available in sqlrdd.ini"
Quit
ElseIf len(aKeys) == 1
nKey := 1
Else
clear screen
@5,1 say PadC( "Choose connection option", 80 )
nKey := achoice( 5, 20, 22, 60, aKeys )
clear screen

     If nKey == 0
        ? &quot;No connection selected&quot;
        Quit
     EndIf
  EndIf

  hDsn := HGetValueAt( hIniFile, nKey )

  If !&quot;CONNSTRING&quot; IN hDsn
     ? &quot;ConnString not found in &quot; + aKeys[nKey]
     Quit
  EndIf

Else
If ! cDatabase IN hIniFile
? "Connection [" + cDatabase + "] not found in sqlrdd.ini"
Quit
EndIf

  hDsn := hIniFile[ cDatabase ]

  If !&quot;CONNSTRING&quot; IN hDsn
     ? &quot;ConnString not found in &quot; + cDatabase
     Quit
  EndIf

EndIf

cConnString := hDsn[ "CONNSTRING" ]
nDetected := DetectDBFromDSN( cConnString )

If nDetected > SYSTEMID_UNKNOW
? "Connecting to", cConnString
nCnn := SR_AddConnection( nDetected, cConnString )
Else
clear screen
nOpt := Alert( "Please, select connection type", { "ODBC", "Postgres", "MySQL", "Oracle", "Firebird" } )
If nOpt > 0
nCnn := SR_AddConnection( If( nOpt = 1, CONNECT_ODBC, if( nOpt = 2, CONNECT_POSTGRES, if( nOpt = 3, CONNECT_MYSQL, if( nOpt = 4, CONNECT_ORACLE, CONNECT_FIREBIRD ) ) ) ), cConnString )
Else
? "No connection type selected"
Quit
EndIf
EndIf

/ returns the connection handle or -1 if it fails /
If nCnn < 0
? "Connection error. See sqlerror.log for details."
Quit
EndIf

Return .T.

/------------------------------------------------------------------------/

Continue the discussion