PostgreSQL Connection String

Just starting out? Need help? Post your questions and find answers here.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

PostgreSQL Connection String

Post by Karbon »

Has anyone out there been able to get PB to with with PostgreSQL's ODBC driver? If so, can you share the connection strings and such you're using?

I have this code to add/remove the DSN's at will:

Code: Select all

Procedure RemoveODBCConnection(Driver.s,DSN.s)
  result=SQLConfigDataSource_(#Database,#ODBC_REMOVE_DSN,Driver,"DSN="+DSN)
  If result
    ProcedureReturn 1
  EndIf
EndProcedure 


Procedure AddODBCConnection(Driver.s,ConnectString.s)
  
  result=SQLConfigDataSource_(#Database,#ODBC_ADD_DSN,Driver,ConnectString )
  
  If result
    ProcedureReturn 1
  Else
    ProcedureReturn 0
  EndIf
  
EndProcedure

Then I do

Code: Select all

AddODBCConnection("PostgreSQL ANSI",connection_string)
but that opens an ODBC driver detail window - I'd like to configure the connection without user intervention (and should be able to since I have all the information for the connection already).

Thanks!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

does this work for you :

Code: Select all

#ODBC_ADD_DSN                       =   1 ; Add Data source
#ODBC_ADD_SYS_DSN                   =   4 ; Add SYSTEM Data source
#ODBC_CONFIG_DSN                    =   2 ; Configure (edit) Data source
#ODBC_REMOVE_DSN                    =   3 ; Remove Data source
#ODBC_REMOVE_SYS_DSN                =   6 ; Remove SYSTEM Data source
#SQL_SUCCESS                        =   0
#SQL_SUCCESS_WITH_INFO              =   1
#SQL_ERROR                          =  -1
#SQL_NO_DATA                        = 100
#SQL_MAX_MESSAGE_LENGTH             = 512
#ODBC_ERROR_GENERAL_ERR             =   1
#ODBC_ERROR_INVALID_BUFF_LEN        =   2
#ODBC_ERROR_INVALID_HWND            =   3
#ODBC_ERROR_INVALID_STR             =   4
#ODBC_ERROR_INVALID_REQUEST_TYPE    =   5
#ODBC_ERROR_COMPONENT_NOT_FOUND     =   6
#ODBC_ERROR_INVALID_NAME            =   7
#ODBC_ERROR_INVALID_KEYWORD_VALUE   =   8
#ODBC_ERROR_INVALID_DSN             =   9
#ODBC_ERROR_INVALID_INF             =  10
#ODBC_ERROR_REQUEST_FAILED          =  11
#ODBC_ERROR_INVALID_PATH            =  12
#ODBC_ERROR_LOAD_LIB_FAILED         =  13
#ODBC_ERROR_INVALID_PARAM_SEQUENCE  =  14
#ODBC_ERROR_INVALID_LOG_FILE        =  15
#ODBC_ERROR_USER_CANCELED           =  16
#ODBC_ERROR_USAGE_UPDATE_FAILED     =  17
#ODBC_ERROR_CREATE_DSN_FAILED       =  18
#ODBC_ERROR_WRITING_SYSINFO_FAILED  =  19
#ODBC_ERROR_REMOVE_DSN_FAILED       =  20
#ODBC_ERROR_OUT_OF_MEM              =  21
#ODBC_ERROR_OUTPUT_STRING_TRUNCATED =  22

Procedure.l MakeKeywordValuePairs(Attributes$)
  ; ConfigDSN Function from M$
  ; http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcconfigdsn.asp
  ; Each pair is terminated with a null byte, and the entire list is terminated with a null byte.
  ; (That is, two null bytes mark the End of the list.) 
  While Right(Attributes$,2)<>";;"
    Attributes$+";"
  Wend
  
  ; Allocate enough memory in both Ascii and Unicode mode + space for the terminating zero character
  *LPAttribMem=AllocateMemory(Len(Attributes$)*SizeOf(character)+SizeOf(character))
  
  ; Copy string to memory
  PokeS(*LPAttribMem,Attributes$,Len(Attributes$))
  
  ; Replace each ';' with zero character
  For L=1 To Len(Attributes$)
    CompilerIf #PB_Compiler_Unicode
      If PeekW(*LPAttribMem + (l-1) * SizeOf(character))=Asc(";")
        PokeW(*LPAttribMem + (l-1) * SizeOf(character),0)
      EndIf
    CompilerElse
      If PeekB(*LPAttribMem + l -1)=Asc(";")
        PokeB(*LPAttribMem + l -1,0)
      EndIf
    CompilerEndIf
  Next
  
  ProcedureReturn *LPAttribMem
EndProcedure

Procedure.b MakeConnection(Driver$,Attributes$)
  *KVPBuffer=MakeKeywordValuePairs(Attributes$)
  
  Result=SQLConfigDataSource_(0,#ODBC_ADD_DSN,Driver$,*KVPBuffer)
  
  FreeMemory(*KVPBuffer)
  
  ProcedureReturn Result
EndProcedure

Procedure.b DeleteConnection(Driver$,DSN$)
  DSN$="DSN="+DSN$
  
  *KVPBuffer=MakeKeywordValuePairs(DSN$)
  
  Result=SQLConfigDataSource_(0,#ODBC_REMOVE_DSN,@Driver$,*KVPBuffer)
  
  FreeMemory(*KVPBuffer)
  
  ProcedureReturn Result
EndProcedure

Debug MakeConnection("PostgreSQL ANSI","DSN=PostgreTest")

Debug DeleteConnection("PostgreSQL ANSI","PostgreTest")
[Edit]
You can tweak the commands by configuring the connection manually in the ODBC Window. It will save a *.DSN file in C:\Program files\Common files\ODBC\Data Sources\*.DSN

Regards Klaus
Last edited by ABBKlaus on Sun Jun 03, 2007 1:43 pm, edited 1 time in total.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Wow, it sure does!!

THANKS!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply