Page 1 of 1

Get Database Name

Posted: Fri Jan 02, 2015 1:03 am
by DarkRookie
Is there a way to get a database's name after you open it.
My search in the help file, forums, and purearea came up with nothing.

I do have a workaround in place but it just seems bleh to me.

I am using ODBC with an Access file if that throws a wrench into anything.
Also using the Demo Version. Which will throw a wrench into things.

Thanks in advance

Re: Get Database Name

Posted: Fri Jan 02, 2015 11:48 am
by nicolaus
Hi,

if you are connectet to the DB, you can get the name with a sql query like this:

Code: Select all

select name from sys.sysdatabases where dbid=db_id()

Re: Get Database Name

Posted: Fri Jan 02, 2015 4:55 pm
by DarkRookie
No dice
Get the following error:

Code: Select all

[Microsoft][ODBC Microsoft Access Driver] Could not find file 'PATH\sys.mdb'.

Re: Get Database Name

Posted: Fri Jan 02, 2015 5:38 pm
by RASHAD
I did that long time back
Hope it will help you
Or just wait for the database guru (Kiffi or ABBKlaus)

Code: Select all

Macro SQL_HANDLE_ENV(Database)
    PeekL(PeekL(IsDatabase(Database))-4)
EndMacro
 
Macro SQL_HANDLE_STMT(Database)
    PeekL(PeekL(IsDatabase(Database)+4)+4)
EndMacro
 
Macro SQL_HANDLE_DBC(Database)
    PeekL(PeekL(IsDatabase(Database)+4))
EndMacro

Procedure.b Add_User_Dsn(Driver$,Attributes$)

  MakeKeywordValuePairs(Attributes$) 
  Result=SQLConfigDataSource_(0,#ODBC_ADD_DSN,Driver$,*LPAttribMem) 
  FreeMemory(*LPAttribMem) 
  ProcedureReturn Result
  
EndProcedure

Procedure.b Delete_User_Dsn(Driver$,DSN$)

  DSN$="DSN="+DSN$ 
  MakeKeywordValuePairs(DSN$) 
  Result=SQLConfigDataSource_(0,#ODBC_REMOVE_DSN,@Driver$,*LPAttribMem) 
  FreeMemory(*LPAttribMem) 
  ProcedureReturn Result
  
EndProcedure

Procedure.b Add_System_Dsn(Driver$,Attributes$)

  MakeKeywordValuePairs(Attributes$) 
  Result=SQLConfigDataSource_(0,#ODBC_ADD_SYS_DSN,Driver$,*LPAttribMem) 
  FreeMemory(*LPAttribMem) 
  ProcedureReturn Result
  
EndProcedure

Procedure.b Delete_System_Dsn(Driver$,DSN$)

  DSN$="DSN="+DSN$ 
  MakeKeywordValuePairs(DSN$) 
  Result=SQLConfigDataSource_(0,#ODBC_REMOVE_SYS_DSN,@Driver$,*LPAttribMem) 
  FreeMemory(*LPAttribMem) 
  ProcedureReturn Result
  
EndProcedure


Procedure.l GetDatabaseTables(Database)

  SQLCancel_(SQL_HANDLE_STMT(Database))
  res.w=SQLTables_(SQL_HANDLE_STMT(Database),0,0,0,0,0,0,0,0)
  If res = 0 Or res = 1                                      ; #SQL_SUCCESS / #SQL_SUCCESS_WITH_INFO
    ProcedureReturn 1
  EndIf
  
EndProcedure


Procedure.l GetDatabaseDBNames(Database)

  SQLCancel_(SQL_HANDLE_STMT(Database))
  res.w=SQLTables_(SQL_HANDLE_STMT(Database),"%",-3,"",-3,"",-3,"",-3)
  If res = 0 Or res = 1                                     ; #SQL_SUCCESS / #SQL_SUCCESS_WITH_INFO
    ProcedureReturn 1
  EndIf
  
EndProcedure


Re: Get Database Name

Posted: Fri Jan 02, 2015 6:54 pm
by DarkRookie
Didn't work for me

It cannot find

Code: Select all

MakeKeywordValuePairs(Attributes$)
Which I am pretty sure is because I am using the Demo and not the full

Re: Get Database Name

Posted: Fri Jan 02, 2015 7:09 pm
by RASHAD
Yes you need the full version for API functions to work

Code: Select all

Procedure.l MakeKeywordValuePairs(Attributes$)
  
  Shared *LPAttribMem
  ; 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


Re: Get Database Name

Posted: Fri Jan 02, 2015 9:10 pm
by Kiffi
the only way i know is to examine the registry.

Image

Here is a module to read the registry from ts-soft:

http://www.purebasic.fr/english/viewtop ... 72#p422572 (works only with the full version).

// Edit:

Code: Select all

Debug Registry::ReadValue(#HKEY_CURRENT_USER, "Software\ODBC\ODBC.INI\AccessTest", "DBQ")
Greetings ... Peter

Re: Get Database Name

Posted: Fri Jan 02, 2015 10:49 pm
by falsam
Sorry for this intervention, but this is strange (for me).
DarkRookie wrote:Is there a way to get a database's name after you open it.
The same question in reverse.

:arrow: after you open it, Is there a way to get a database's name ?

To open a database, you must provide the name of this database?

Re: Get Database Name

Posted: Fri Jan 02, 2015 11:40 pm
by infratec
@falsam

no, that's not true for ODBC.
You open the ODBC connection with the name of the connection.
You don't know the name of the database.
Look at the pictures of Kiffi:

You open 'AccessTest', but the database which is behind is Test.mdb

Bernd

Re: Get Database Name

Posted: Fri Jan 02, 2015 11:53 pm
by falsam
ok. Thank infratec :)

Re: Get Database Name

Posted: Tue Sep 08, 2015 10:07 pm
by fsw
Using the code from RASHAD for a while now (a 64bit version with int size 8 not 4).
With PB5.31 this code works beautifully.
With PB5.40 beta 3 this code gives me the #SQL_INVALID_HANDLE error (-2).

Would be nice if someone could test it.