Get Database Name

Just starting out? Need help? Post your questions and find answers here.
User avatar
DarkRookie
User
User
Posts: 20
Joined: Wed Nov 24, 2010 5:47 pm

Get Database Name

Post 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
Last edited by DarkRookie on Fri Jan 02, 2015 6:53 pm, edited 1 time in total.
It is better to have a dumb question than a wrong answer!
nicolaus
Enthusiast
Enthusiast
Posts: 456
Joined: Tue Aug 05, 2003 11:30 pm
Contact:

Re: Get Database Name

Post 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()
User avatar
DarkRookie
User
User
Posts: 20
Joined: Wed Nov 24, 2010 5:47 pm

Re: Get Database Name

Post by DarkRookie »

No dice
Get the following error:

Code: Select all

[Microsoft][ODBC Microsoft Access Driver] Could not find file 'PATH\sys.mdb'.
It is better to have a dumb question than a wrong answer!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Database Name

Post 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

Egypt my love
User avatar
DarkRookie
User
User
Posts: 20
Joined: Wed Nov 24, 2010 5:47 pm

Re: Get Database Name

Post 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
It is better to have a dumb question than a wrong answer!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Get Database Name

Post 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

Egypt my love
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Get Database Name

Post 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
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Get Database Name

Post 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?

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get Database Name

Post 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
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Get Database Name

Post by falsam »

ok. Thank infratec :)

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Re: Get Database Name

Post 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.

I am to provide the public with beneficial shocks.
Alfred Hitshock
Post Reply