Windows Database Handle

Just starting out? Need help? Post your questions and find answers here.
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Windows Database Handle

Post by Xombie »

I've been using this trick (from ABBKlaus) to get the handle to a database...

Code: Select all

HandleDB = PeekL(IsDatabase(iDatabase) + 4)
...but now my SQLGetDiagRec() function is returning a -2 which (I think) means #SQL_INVALID_HANDLE.

Did anything change with the latest beta to invalidate this method?
Last edited by Xombie on Fri Oct 05, 2007 9:45 pm, edited 3 times in total.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You are messing with internals... :) Yes we changed that, try to peek between 0 and 16 (+0, +4, +8, +16) it should be somewhere here.
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

try this :

Code: Select all

  If OpenDatabase(1,"DB-Test","Admin","")
    ID=IsDatabase(1)

    Structure PB_StructureDataBase
	    Connection.l   ; OS ODBC Connection identifier  (SQLHDBC)
	    Statement.l    ; OS ODBC Statement identifier  (SQLHSTMT)
    EndStructure

    *Buffer.PB_StructureDataBase=ID
    
    Handle=PeekL(*Buffer\Statement)
    
    res.w=SQLAllocHandle_(3,Handle,@stmthandle)
    If res = #SQL_SUCCESS
      Debug "stmthandle="+Str(stmthandle)
    EndIf
    
    res.w=SQLTables_(stmthandle,0,0,0,0,0,0,0,0)
    If res=#SQL_INVALID_HANDLE
      Debug "#SQL_INVALID_HANDLE"
    EndIf
    
    If res = #SQL_SUCCESS Or res = #SQL_SUCCESS_WITH_INFO 
      Debug "#SQL_SUCCESS" 
      strlen=128+1 
      *Buffer1=AllocateMemory(strlen):strlen1.l=0 
      *Buffer2=AllocateMemory(strlen):strlen2.l=0 
      *Buffer3=AllocateMemory(strlen):strlen3.l=0 
      *Buffer4=AllocateMemory(strlen):strlen4.l=0 
      *Buffer5=AllocateMemory(strlen):strlen5.l=0 
      
      #SQL_C_CHAR=1
      SQLBindCol_(stmthandle,1,#SQL_C_CHAR,*Buffer1,strlen,@strlen1) ; Database
      ;SQLBindCol_(stmthandle,2,#SQL_C_CHAR,*Buffer2,strlen,@strlen2) ; ?
      SQLBindCol_(stmthandle,3,#SQL_C_CHAR,*Buffer3,strlen,@strlen3) ; Tablename
      SQLBindCol_(stmthandle,4,#SQL_C_CHAR,*Buffer4,strlen,@strlen4) ; Table-Type
      
      While SQLFetch_(stmthandle)&$FFFF=0 
        Debug PeekS(*Buffer1,-1,#PB_Ascii)
        Debug PeekS(*Buffer3,-1,#PB_Ascii)
        Debug PeekS(*Buffer4,-1,#PB_Ascii)
        Debug "-----------------------------------------------------------------"
      Wend 
    EndIf    
    
    If SQLFreeHandle_(3,stmthandle)=#SQL_SUCCESS
      stmthandle=0
    EndIf
  EndIf
Regards Klaus
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Now, fred. You know you're supposed to get my permission before changing PB internal stuff :(

So this is not a bug (no surprise). Feel free to move this to general or coding questions where everyone can shake their head at my ineptitude :)

@ABBKlaus - I tried running your code but I get a "Cannot Execute ......" error statement. The code does not compile or execute.

I also tried adapting the bits to fit my need. I went all the way to...

Code: Select all

res.w=SQLAllocHandle_(3,Handle,@stmthandle)
... to get the handle and passed that to my SQLGetDiagRec() function and now I'm just getting a result of 100 which should be #SQL_NO_DATA.

The function I'm using is a badly homegrown...

Code: Select all

Procedure.s GetSQLMessages(HandleDB.l) 
   ;
   Protected lResult.l
   ;
   Protected SQLState.s = Space(6)
   ;
   Protected NativeErrorPtr.l
   ;
   Protected MessageText.s = Space(10000)
   ;
   Protected BufferLength.l = 10000
   ;
   Protected TextLengthPtr.l
   ;
   Protected DiagInfoPtr.l = -1
   ;
   Repeat
      ;
      lResult = SQLGetDiagRec(#SQL_HANDLE_STMT, HandleDB, 1, @SQLState, @NativeErrorPtr, @MessageText, BufferLength, @TextLengthPtr)
      ;
      Debug lResult
      ;
      If lResult = #SQL_NO_DATA : Break : EndIf
      ;
      Debug Str(TextLengthPtr)+" : "+SQLState+" : "+Left(MessageText, 20)+" : "+Str(lResult)
      ;
      SQLMoreResults(HandleDB)
      ;
   ForEver
   ;
   ProcedureReturn ""
   ;
EndProcedure
And did work in the past. I tested using this bit from Wikipedia.

Code: Select all

DECLARE @Counter INT
SET @Counter = 10
WHILE @Counter > 0
BEGIN
   PRINT 'The count is ' + CONVERT(VARCHAR(10), @Counter)
   SET @Counter = @Counter - 1
END
And in the past it worked. However, ever since fred cruelly changed how PB worked (without asking!) I get nothing returned.

In this most recent test I'm passing stmthandle to the GetSQLMessages function.

Help?
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

@xombie, the structure i used was from PB4.02 and is not valid anymore for PB4.10B3.

could you try this :

Code: Select all

    ID=IsDatabase(1)
    Debug "ID="+Str(ID)
    
    Debug "SQL_HANDLE_ENV="+Hex(PeekL(PeekL(ID)-4))
    Debug "SQL_HANDLE_STMT="+Hex(PeekL(PeekL(ID+4)+4))
    Debug "SQL_HANDLE_DBC="+Hex(PeekL(PeekL(ID+4)))
i uploaded a test program to demonstrate : DSN-Test

Regards Klaus
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

That worked like a charm, thanks! And as a side bonus, I got the three different handles :) I was wondering how I might get those but didn't need them yet.

Thanks again!
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

but be warned, they change from beta to beta :twisted:
(just tested Beta4 and it worked)

Regards Klaus
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Fred wrote:You are messing with internals... :) Yes we changed that, try to peek between 0 and 16 (+0, +4, +8, +16) it should be somewhere here.
Just got burned because Fantaisie software changed the internals.

Why are you guys making it hard to get the handle?

Yes, PureBasic is soo powerful, you can even use Windows API with ease.
You want to use Windows DataBase API? No problem :!:
Oh, it doesn't work anymore?
We are sorry, but we changed some internal stuff.
It's our compiler and our libraries, so we are allowed to do that.
Sorry, we don't care enough to create a GetDataBaseHandle command :evil:

:arrow:
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

You could always request that command... 8)
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

fsw: Please, refrain from posting if the only purpose of it is to whine. Thank you.

Hopefully one can learn a lot from posts like this. Even the makers of the software so why the silly sarcasm? :?
Last edited by byo on Mon Dec 03, 2007 11:26 pm, edited 1 time in total.
Proud registered Purebasic user.
Because programming should be fun.
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post by Dare »

Xombie wrote:Now, fred. You know you're supposed to get my permission before changing PB internal stuff
:D


I just love it that so many people here have a great sense of humour!
Dare2 cut down to size
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Sorry, we don't care enough to create a GetDataBaseHandle command
You never even posted a request for one... who was it again who doesn't care enough?
BERESHEIT
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

Hey guys,
Fred's response is from October 5th, so I thought he would think by itself of it, but he didn't.
He would have had enough time to implement this in 5 minutes before releasing 4.1 :wink:
Would have been common sense, would it not?

Also if you would have read my post you would see I found out about it today.
But you guys are right, I could put in a request.

fsw

BTW: @byo
Maybe you don't know, but I'm with PB since spring 2001 and have contributed a lot to PB, specially when PB only had a handful of users.
You joined 01 Apr 2007 :shock:, so what was your request again?
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

BTW: @byo
Maybe you don't know, but I'm with PB since spring 2001 and have contributed a lot to PB, specially when PB only had a handful of users.
You joined 01 Apr 2007 :shock:, so what was your request again?
OK, let's end it here before this topic turns into childish better-than-you arguments. All I say is help the guy who posted the question in a Coding Questions' topic or just don't. And then make a new thread on the Offtopic section on how it's easy to program something in assembly in 5 minutes.

I'm sorry to have reacted like I did but the kind of sarcastic posts like the one you posted really get on my nerves. And I'm sure I don't speak only for myself.

Peace, man.
Proud registered Purebasic user.
Because programming should be fun.
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post by fsw »

byo wrote:OK, let's end it here before this topic turns into childish better-than-you arguments.
You are right, and of top of that I overreacted myself, sorry.

Maybe next time when I discover something obvious to me (like useful customer workarounds don't work anymore, lets make a official function out of it... what about UBound?) and also a very important function, I will sit on my fingers and let some hours pass before I write something.

Peace :wink:
Post Reply